<?php

require __DIR__ . '/vendor/autoload.php';

$app = require_once __DIR__ . '/bootstrap/app.php';
$app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();

// Get Stripe key
$gateway = App\Models\PaymentGateway::where('provider', 'stripe')->first();
$secretKey = $gateway->is_live_mode ? $gateway->live_secret_key : $gateway->test_secret_key;

\Stripe\Stripe::setApiKey($secretKey);

echo "Testing Stripe Invoice Creation\n";
echo "================================\n\n";

try {
    // Get a test user
    $user = App\Models\User::first();
    
    if (!$user) {
        echo "No users found in database\n";
        exit(1);
    }
    
    echo "User: {$user->name} ({$user->email})\n\n";
    
    // Create or get Stripe customer
    $customer = null;
    if ($user->stripe_customer_id) {
        try {
            $customer = \Stripe\Customer::retrieve($user->stripe_customer_id);
            echo "Found existing Stripe customer: {$customer->id}\n";
        } catch (\Exception $e) {
            echo "Customer not found, will create new one\n";
        }
    }
    
    if (!$customer) {
        $customer = \Stripe\Customer::create([
            'email' => $user->email,
            'name' => $user->name,
            'metadata' => ['user_id' => $user->id],
        ]);
        $user->update(['stripe_customer_id' => $customer->id]);
        echo "Created new Stripe customer: {$customer->id}\n";
    }
    
    echo "\n--- Creating Invoice ---\n";
    
    $amount = 50.00; // $50.00
    $amountInCents = (int)($amount * 100);
    
    echo "Amount: \${$amount}\n";
    echo "Amount in cents: {$amountInCents}\n\n";
    
    // Create invoice item
    $invoiceItem = \Stripe\InvoiceItem::create([
        'customer' => $customer->id,
        'amount' => $amountInCents,
        'currency' => 'usd',
        'description' => 'Test Support Service',
    ]);
    
    echo "Invoice Item Created: {$invoiceItem->id}\n";
    echo "Invoice Item Amount: {$invoiceItem->amount} cents\n\n";
    
    // Create invoice
    $invoice = \Stripe\Invoice::create([
        'customer' => $customer->id,
        'auto_advance' => true,
        'collection_method' => 'send_invoice',
        'days_until_due' => 30,
        'metadata' => [
            'user_id' => $user->id,
            'test' => 'true',
        ],
    ]);
    
    echo "Invoice Created: {$invoice->id}\n";
    echo "Status: {$invoice->status}\n";
    echo "Amount Due (before finalize): {$invoice->amount_due} cents\n";
    echo "Total (before finalize): {$invoice->total} cents\n\n";
    
    // Finalize the invoice
    $invoice->finalizeInvoice();
    
    echo "--- Invoice Finalized ---\n";
    
    // Retrieve again to get updated values
    $invoice = \Stripe\Invoice::retrieve($invoice->id);
    
    echo "Status: {$invoice->status}\n";
    echo "Amount Due: {$invoice->amount_due} cents (\$" . ($invoice->amount_due / 100) . ")\n";
    echo "Total: {$invoice->total} cents (\$" . ($invoice->total / 100) . ")\n";
    echo "Subtotal: {$invoice->subtotal} cents (\$" . ($invoice->subtotal / 100) . ")\n";
    echo "Hosted Invoice URL: {$invoice->hosted_invoice_url}\n";
    echo "Invoice PDF: {$invoice->invoice_pdf}\n\n";
    
    echo "✓ Test completed successfully!\n";
    echo "\nYou can view the invoice at:\n{$invoice->hosted_invoice_url}\n";
    
} catch (\Exception $e) {
    echo "ERROR: " . $e->getMessage() . "\n";
    echo $e->getTraceAsString() . "\n";
}
