<?php

/**
 * Manual Test Script for Inactive profile warning Emails
 *
 * Usage in tinker:
 * php artisan tinker
 * include 'send-test-warnings.php';
 * sendTestWarnings(102); // Replace 102 with your user ID
 */

function sendTestWarnings($userId) {
    $user = \App\Models\User::find($userId);

    if (!$user) {
        echo "❌ User not found with ID: {$userId}\n";
        return;
    }

    echo "  Sending test warning emails for user: {$user->name} (ID: {$userId})\n";
    echo "   Email: {$user->email}\n";
    echo "   Language: {$user->lang_preference}\n\n";

    // Get accounts and balances
    $accounts = [];
    $totalBalance = 0;
    $profileAccounts = $user->accounts()->active()->notRemoved()->get();

    foreach ($profileAccounts as $account) {
        // Clear cache to get fresh balance
        \Cache::forget("account_balance_{$account->id}");

        $balance = $account->balance; // Property, not method
        $totalBalance += $balance;
        $accounts[] = [
            'id' => $account->id,
            'name' => $account->name,
            'balance' => $balance,
            'balanceFormatted' => tbFormat($balance),
        ];
    }

    // Test data for each warning level
    $warnings = [
        'warning_1' => [
            'class' => \App\Mail\InactiveProfileWarning1Mail::class,
            'timeRemaining' => '2 weeks',
            'daysRemaining' => 14,
            'daysSinceLogin' => 351,
        ],
        'warning_2' => [
            'class' => \App\Mail\InactiveProfileWarning2Mail::class,
            'timeRemaining' => '1 week',
            'daysRemaining' => 7,
            'daysSinceLogin' => 358,
        ],
        'warning_final' => [
            'class' => \App\Mail\InactiveProfileWarningFinalMail::class,
            'timeRemaining' => '24 hours',
            'daysRemaining' => 1,
            'daysSinceLogin' => 365,
        ],
    ];

    // Send all three warning emails
    foreach ($warnings as $warningType => $data) {
        echo "   Dispatching {$warningType}...\n";

        $mailClass = $data['class'];
        \Illuminate\Support\Facades\Mail::to($user->email)->queue(
            new $mailClass(
                $user,
                'User',
                $data['timeRemaining'],
                $data['daysRemaining'],
                $accounts,
                $totalBalance,
                $data['daysSinceLogin']
            )
        );
    }

    echo "\n✅ All warning emails dispatched to queue\n";
    echo "   Total balance: " . tbFormat($totalBalance) . "\n";
    echo "   Accounts: " . count($accounts) . "\n\n";
    echo "🚀 Processing queue...\n";

    // Process the queue to actually send the emails
    \Illuminate\Support\Facades\Artisan::call('queue:work', [
        '--stop-when-empty' => true,
        '--tries' => 1,
    ]);

    echo "✅ Queue processed. Check your inbox at: {$user->email}\n";
}

// Example usage (uncomment to run):
// sendTestWarnings(102);

echo "
╔════════════════════════════════════════════════════════════╗
║  Inactive profile warning Email Test Script               ║
╚════════════════════════════════════════════════════════════╝

Usage:
  sendTestWarnings(102);  // Replace 102 with user ID

This will send all 3 warning emails:
  • Warning 1 (2 weeks remaining)
  • Warning 2 (1 week remaining)
  • Warning Final (24 hours remaining)

";
