<?php

require_once 'vendor/autoload.php';

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

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

echo "=== Testing Enhanced Approval Display ===\n\n";

try {
    // Find the ZIKO TEAM joint account
    $jointAccount = JointAccount::where('account_name', 'ZIKO TEAM')->first();
    
    if (!$jointAccount) {
        echo "ZIKO TEAM joint account not found.\n";
        exit;
    }
    
    echo "Joint Account: {$jointAccount->account_name}\n";
    echo "Active Members: " . $jointAccount->activeMembers()->count() . "\n\n";
    
    // Get all withdrawal transactions with approvals
    $withdrawalTransactions = JointAccountTransaction::where('joint_account_id', $jointAccount->id)
        ->where('transaction_type', 'withdrawal')
        ->with(['initiator', 'approvals.approver'])
        ->latest()
        ->get();
    
    echo "=== Withdrawal Transactions with Approval Details ===\n\n";
    
    foreach ($withdrawalTransactions 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 "Description: {$transaction->description}\n";
        echo "Reference: {$transaction->reference}\n";
        
        $approvalCount = $transaction->approvals->where('action', 'approved')->count();
        $rejectionCount = $transaction->approvals->where('action', 'rejected')->count();
        $totalApprovals = $transaction->approvals->count();
        $requiredApprovals = $jointAccount->activeMembers()->count();
        
        echo "Required Approvals: {$requiredApprovals}\n";
        echo "Received Approvals: {$approvalCount}\n";
        echo "Rejections: {$rejectionCount}\n";
        echo "Total Approval Records: {$totalApprovals}\n";
        
        if ($transaction->approvals->count() > 0) {
            echo "Approval Details:\n";
            foreach ($transaction->approvals as $approval) {
                $status = $approval->action === 'approved' ? '✅ APPROVED' : '❌ REJECTED';
                echo "  - {$approval->approver->name}: {$status}\n";
                if ($approval->comment) {
                    echo "    Comment: {$approval->comment}\n";
                }
                echo "    Date: {$approval->action_at}\n";
            }
        }
        
        // Show approval progress
        $progressPercentage = $requiredApprovals > 0 ? ($approvalCount / $requiredApprovals) * 100 : 0;
        echo "Approval Progress: {$progressPercentage}% ({$approvalCount}/{$requiredApprovals})\n";
        
        if ($transaction->status === 'pending') {
            if ($approvalCount >= $requiredApprovals) {
                echo "Status: 🟢 READY FOR ADMIN PROCESSING\n";
            } else {
                echo "Status: 🟡 WAITING FOR MORE MEMBER APPROVALS\n";
            }
        } else {
            echo "Status: 🔵 {$transaction->status}\n";
        }
        
        echo "\n" . str_repeat("-", 50) . "\n\n";
    }
    
    echo "=== Test Complete ===\n";
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
    echo "Stack trace: " . $e->getTraceAsString() . "\n";
} 