<?php
require 'vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

$pdo = new PDO(
    'mysql:host='.$_ENV['DB_HOST'].';dbname='.$_ENV['DB_DATABASE'],
    $_ENV['DB_USERNAME'],
    $_ENV['DB_PASSWORD']
);

echo "=== All Invoices for User 17 ===\n\n";

$stmt = $pdo->query('
    SELECT i.id, i.subscription_id, i.invoice_number, i.amount, i.status, 
           i.paid_at, i.created_at, s.stripe_subscription_id
    FROM invoices i
    LEFT JOIN subscriptions s ON i.subscription_id = s.id
    WHERE i.user_id = 17
    ORDER BY i.id ASC
');

$count = 0;
while ($inv = $stmt->fetch()) {
    $count++;
    echo "Invoice #{$inv['id']}\n";
    echo "  Subscription ID: {$inv['subscription_id']}\n";
    echo "  Stripe Sub ID: {$inv['stripe_subscription_id']}\n";
    echo "  Invoice Number: {$inv['invoice_number']}\n";
    echo "  Amount: \${$inv['amount']}\n";
    echo "  Status: {$inv['status']}\n";
    echo "  Paid At: {$inv['paid_at']}\n";
    echo "  Created At: {$inv['created_at']}\n";
    echo "\n";
}

echo "Total Invoices: $count\n\n";

echo "=== Current Active Subscription ===\n";
$stmt = $pdo->query('SELECT id, stripe_subscription_id, status, created_at FROM subscriptions WHERE user_id = 17 ORDER BY id DESC LIMIT 1');
$sub = $stmt->fetch();
if ($sub) {
    echo "Subscription ID: {$sub['id']}\n";
    echo "Stripe ID: {$sub['stripe_subscription_id']}\n";
    echo "Status: {$sub['status']}\n";
    echo "Created: {$sub['created_at']}\n";
}
