<?php

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

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

use App\Models\ImmigoProSubscription;
use App\Models\ImmigoProInstallmentPayment;

echo "=== Checking Subscription and Installment Status ===\n\n";

// Get all active subscriptions
$subscriptions = ImmigoProSubscription::where('status', 'active')
    ->orderBy('updated_at', 'desc')
    ->get();

if ($subscriptions->isEmpty()) {
    echo "No active subscriptions found.\n";
    exit;
}

foreach ($subscriptions as $sub) {
    echo "Subscription ID: {$sub->id}\n";
    echo "User ID: {$sub->user_id}\n";
    echo "Package: {$sub->package_name}\n";
    echo "Payment Plan: {$sub->payment_plan}\n";
    echo "Status: {$sub->status}\n";
    echo "Total Installments: {$sub->installments}\n";
    echo "Paid Installments: {$sub->paid_installments}\n";
    echo "Next Charge Date: " . ($sub->next_charge_date ?? 'NULL') . "\n";
    echo "Updated At: {$sub->updated_at}\n";
    
    // Get installment payments
    $payments = ImmigoProInstallmentPayment::where('subscription_id', $sub->id)
        ->orderBy('installment_number', 'asc')
        ->get();
    
    if ($payments->isEmpty()) {
        echo "Installment Payments: None recorded\n";
    } else {
        echo "\nInstallment Payments:\n";
        foreach ($payments as $payment) {
            echo "  - Installment #{$payment->installment_number}: ";
            echo "\${$payment->amount} - {$payment->status}";
            echo " (Paid: " . ($payment->paid_at ?? 'N/A') . ")\n";
        }
    }
    
    echo "\n" . str_repeat('-', 60) . "\n\n";
}
