<?php

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

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

echo "=== Complete Payment Flow Verification ===\n\n";

// 1. Check Invoice
$invoice = App\Models\SupportInvoice::latest()->first();
if (!$invoice) {
    echo "❌ No invoices found\n";
    exit(1);
}

echo "📄 Latest Invoice:\n";
echo "   ID: {$invoice->id}\n";
echo "   User: {$invoice->user->name}\n";
echo "   Amount: \${$invoice->amount}\n";
echo "   Currency: " . strtoupper($invoice->currency) . "\n";
echo "   Status: {$invoice->status}\n";
echo "   Stripe ID: {$invoice->stripe_invoice_id}\n";
echo "   URL: {$invoice->hosted_invoice_url}\n";
echo "   Created: {$invoice->created_at}\n";
if ($invoice->paid_at) {
    echo "   Paid At: {$invoice->paid_at}\n";
}
echo "\n";

// 2. Check Transactions
$transactions = App\Models\InvoiceTransaction::where('support_invoice_id', $invoice->id)->get();
echo "💰 Transactions for this invoice: " . $transactions->count() . "\n";
foreach ($transactions as $tx) {
    echo "   - Transaction ID: {$tx->transaction_id}\n";
    echo "     Amount: \${$tx->amount}\n";
    echo "     Status: {$tx->status}\n";
    echo "     Payment Method: {$tx->payment_method}\n";
    if ($tx->payment_method_details) {
        $details = $tx->payment_method_details;
        echo "     Card: " . ($details['card_brand'] ?? 'N/A') . " ending in " . ($details['card_last4'] ?? 'N/A') . "\n";
    }
    echo "     Paid At: {$tx->paid_at}\n";
}
echo "\n";

// 3. Flow Summary
echo "🔄 Payment Flow Status:\n";
echo "   ✓ Invoice Created: Yes\n";
echo "   ✓ Invoice Finalized: " . ($invoice->status === 'open' ? "Yes (awaiting payment)" : $invoice->status) . "\n";
echo "   ✓ Payment URL Generated: Yes\n";
echo "   ✓ Amount Correct: \${$invoice->amount} (check Stripe URL to verify)\n";

if ($transactions->count() > 0) {
    echo "   ✓ Payment Received: Yes\n";
    echo "   ✓ Transaction Recorded: Yes\n";
    echo "   ✓ Webhook Processed: Yes\n";
    echo "\n   🎉 COMPLETE FLOW WORKING!\n";
} else {
    echo "   ⏳ Payment Received: Not yet\n";
    echo "   ⏳ Transaction Recorded: Waiting for payment\n";
    echo "   ⏳ Webhook Processed: Waiting for payment\n";
    echo "\n   📝 Next Steps:\n";
    echo "   1. Open this URL in browser:\n";
    echo "      {$invoice->hosted_invoice_url}\n\n";
    echo "   2. Pay using test card:\n";
    echo "      Card: 4242 4242 4242 4242\n";
    echo "      Expiry: Any future date (e.g., 12/25)\n";
    echo "      CVC: Any 3 digits (e.g., 123)\n";
    echo "      ZIP: Any 5 digits (e.g., 12345)\n\n";
    echo "   3. After payment, run this script again to verify:\n";
    echo "      php verify_payment_flow.php\n\n";
    echo "   4. Or check database:\n";
    echo "      SELECT * FROM invoice_transactions WHERE support_invoice_id = {$invoice->id};\n";
}

echo "\n=== Webhook Configuration ===\n";
echo "Endpoint: " . config('app.url') . "/api/v1/payments/webhooks/stripe\n";
echo "Events: invoice.paid, invoice.payment_succeeded\n\n";

// Check webhook secret
$gateway = App\Models\PaymentGateway::where('provider', 'stripe')->first();
if ($gateway && $gateway->test_webhook_secret) {
    echo "✓ Webhook secret configured\n";
} else {
    echo "⚠️  Warning: Webhook secret not configured\n";
    echo "   This may cause webhook verification to fail\n";
}

echo "\n=== Database Tables ===\n";
echo "support_invoices: " . App\Models\SupportInvoice::count() . " records\n";
echo "invoice_transactions: " . App\Models\InvoiceTransaction::count() . " records\n";
