<?php

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

$app = require_once __DIR__ . '/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$request = Illuminate\Http\Request::create('/');
$kernel->handle($request);

echo "=== Testing User Detail Endpoint ===" . PHP_EOL . PHP_EOL;

// Get a subscription ID to test with
$subscription = DB::table('immigo_pro_subscriptions')
    ->where('status', 'active')
    ->first();

if (!$subscription) {
    echo "No active subscriptions found!" . PHP_EOL;
    exit(1);
}

echo "Testing with Subscription ID: " . $subscription->id . PHP_EOL;
echo "User ID: " . $subscription->user_id . PHP_EOL;
echo "License Key: " . ($subscription->license_key ?? 'Not set') . PHP_EOL;
echo PHP_EOL;

// Create controller instance
$controller = new \App\Http\Controllers\Api\V1\AdminImmigoProQuotationController();

// Call the method
try {
    $response = $controller->getUserQuotationAnswers($subscription->id);
    $data = $response->getData();
    
    echo "Response Status: " . $response->getStatusCode() . PHP_EOL;
    echo PHP_EOL;
    
    if (isset($data->subscription)) {
        echo "=== Subscription Data ===" . PHP_EOL;
        echo "Package: " . $data->subscription->package_name . PHP_EOL;
        echo "Payment Plan: " . $data->subscription->payment_plan . PHP_EOL;
        echo "Status: " . $data->subscription->status . PHP_EOL;
        echo "License Key: " . ($data->subscription->license_key ?? 'Not set') . PHP_EOL;
        echo "Base Price: $" . ($data->subscription->base_price ?? '0') . PHP_EOL;
        echo "Upfront Amount: $" . ($data->subscription->upfront_amount ?? '0') . PHP_EOL;
        echo "Monthly Amount: $" . ($data->subscription->monthly_amount ?? '0') . PHP_EOL;
        echo "Total Amount: $" . ($data->subscription->total_amount ?? '0') . PHP_EOL;
        echo "Installments: " . ($data->subscription->paid_installments ?? 0) . " / " . ($data->subscription->installments ?? 0) . PHP_EOL;
        echo PHP_EOL;
    }
    
    if (isset($data->user)) {
        echo "=== User Data ===" . PHP_EOL;
        echo "Name: " . $data->user->first_name . " " . $data->user->last_name . PHP_EOL;
        echo "Email: " . $data->user->email . PHP_EOL;
        echo PHP_EOL;
    }
    
    if (isset($data->quotation_answers)) {
        echo "=== Quotation Answers ===" . PHP_EOL;
        echo "Total Answers: " . count($data->quotation_answers) . PHP_EOL;
        echo PHP_EOL;
    }
    
    if (isset($data->payments)) {
        echo "=== Payment Details ===" . PHP_EOL;
        echo "Total Payments: " . count($data->payments) . PHP_EOL;
        foreach ($data->payments as $index => $payment) {
            echo "Payment #" . ($index + 1) . ": $" . $payment->amount . " - " . $payment->status . " (Due: " . $payment->due_date . ")" . PHP_EOL;
        }
        echo PHP_EOL;
    }
    
    echo "✓ API endpoint working correctly!" . PHP_EOL;
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . PHP_EOL;
    echo "Trace: " . $e->getTraceAsString() . PHP_EOL;
}
