<?php

require_once 'vendor/autoload.php';

use App\Models\User;
use App\Models\JointAccount;
use App\Models\JointAccountTransaction;

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

echo "=== Testing Admin Interface API ===\n\n";

try {
    // Find an admin user
    $adminUser = User::whereHas('roles', function($query) {
        $query->where('role', 'admin');
    })->first();
    
    if (!$adminUser) {
        echo "No admin user found.\n";
        exit;
    }
    
    echo "Admin User: {$adminUser->name} ({$adminUser->email})\n";
    echo "Is Admin: " . ($adminUser->isAdmin() ? 'Yes' : 'No') . "\n\n";
    
    // Find processing transactions
    $processingTransactions = JointAccountTransaction::where('transaction_type', 'withdrawal')
        ->where('status', 'processing')
        ->with([
            'jointAccount.members.user',
            'initiator',
            'approvals.approver'
        ])
        ->get();
    
    echo "Processing Transactions Found: " . $processingTransactions->count() . "\n\n";
    
    foreach ($processingTransactions as $transaction) {
        echo "Transaction ID: {$transaction->id}\n";
        echo "Amount: \${$transaction->amount}\n";
        echo "Status: {$transaction->status}\n";
        echo "Initiator: {$transaction->initiator->name}\n";
        echo "Joint Account: {$transaction->jointAccount->account_name}\n";
        echo "Approvals: " . $transaction->approvals->count() . "\n";
        
        foreach ($transaction->approvals as $approval) {
            echo "  - {$approval->approver->name}: {$approval->action}\n";
        }
        echo "\n";
    }
    
    // Test the admin controller method
    echo "=== Testing Admin Controller Method ===\n";
    
    // Simulate the admin pending withdrawals method
    $controller = new \App\Http\Controllers\JointAccountTransactionController();
    $reflection = new ReflectionClass($controller);
    $adminPendingWithdrawalsMethod = $reflection->getMethod('adminPendingWithdrawals');
    $adminPendingWithdrawalsMethod->setAccessible(true);
    
    // Set the authenticated user
    \Illuminate\Support\Facades\Auth::login($adminUser);
    
    // Call the method
    $response = $adminPendingWithdrawalsMethod->invoke($controller);
    $data = json_decode($response->getContent(), true);
    
    echo "API Response Status: " . $data['status'] . "\n";
    echo "Total Pending: " . $data['total_pending'] . "\n";
    
    if (isset($data['pending_withdrawals'])) {
        foreach ($data['pending_withdrawals'] as $jointAccountId => $transactions) {
            echo "Joint Account ID: {$jointAccountId}\n";
            echo "Transactions: " . count($transactions) . "\n";
            
            foreach ($transactions as $transaction) {
                echo "  - Transaction ID: {$transaction['id']}\n";
                echo "    Amount: \${$transaction['amount']}\n";
                echo "    Status: {$transaction['status']}\n";
                echo "    Initiator: {$transaction['initiator']['name']}\n";
                echo "    Approvals: " . count($transaction['approvals']) . "\n";
            }
        }
    }
    
    echo "\n=== Test Complete ===\n";
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
    echo "Stack trace: " . $e->getTraceAsString() . "\n";
} 