<?php

require_once 'vendor/autoload.php';

use App\Models\User;
use App\Models\JointAccount;
use App\Models\JointAccountMember;

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

echo "=== Specific User and Joint Account Test ===\n\n";

try {
    // Check the specific users mentioned in the database
    $user23 = User::find(23);
    $user362 = User::find(362);
    $user1 = User::find(1);
    $userUloubi = User::where('email', 'uloubi7@gmail.com')->first();
    
    echo "=== Users ===\n";
    if ($user23) {
        echo "User 23: {$user23->email} - {$user23->name}\n";
    } else {
        echo "User 23: NOT FOUND\n";
    }
    
    if ($user362) {
        echo "User 362: {$user362->email} - {$user362->name}\n";
    } else {
        echo "User 362: NOT FOUND\n";
    }
    
    if ($user1) {
        echo "User 1: {$user1->email} - {$user1->name}\n";
    } else {
        echo "User 1: NOT FOUND\n";
    }
    
    if ($userUloubi) {
        echo "User uloubi7@gmail.com: ID {$userUloubi->id} - {$userUloubi->name}\n";
    } else {
        echo "User uloubi7@gmail.com: NOT FOUND\n";
    }
    
    echo "\n=== Joint Account Details ===\n";
    
    // Get the specific joint account
    $jointAccount = JointAccount::find(1);
    if ($jointAccount) {
        echo "Joint Account: {$jointAccount->account_name} (ID: {$jointAccount->id})\n";
        echo "  Status: {$jointAccount->status}\n";
        echo "  Balance: \${$jointAccount->balance}\n";
        echo "  Created by: {$jointAccount->created_by}\n";
        
        // Get creator details
        $creator = User::find($jointAccount->created_by);
        if ($creator) {
            echo "  Creator: {$creator->email} - {$creator->name}\n";
        }
        
        echo "  Members:\n";
        $members = JointAccountMember::where('joint_account_id', $jointAccount->id)
            ->with('user')
            ->get();
            
        foreach ($members as $member) {
            $userEmail = $member->user ? $member->user->email : 'Unknown';
            $userName = $member->user ? $member->user->name : 'Unknown';
            echo "    - {$userEmail} (Role: {$member->role}, Status: {$member->status})\n";
        }
    } else {
        echo "Joint Account ID 1: NOT FOUND\n";
    }
    
    echo "\n=== Testing Specific User Access ===\n";
    
    // Test user 23 (should be owner)
    if ($user23) {
        $user23Active = JointAccount::whereHas('members', function($query) use ($user23) {
            $query->where('user_id', $user23->id)->where('status', 'active');
        })->get();
        
        $user23Pending = JointAccount::whereHas('members', function($query) use ($user23) {
            $query->where('user_id', $user23->id)->where('status', 'pending');
        })->get();
        
        echo "User 23 ({$user23->email}):\n";
        echo "  Active accounts: {$user23Active->count()}\n";
        echo "  Pending invitations: {$user23Pending->count()}\n";
    }
    
    // Test user uloubi7@gmail.com (should have pending invitation)
    if ($userUloubi) {
        $userUloubiActive = JointAccount::whereHas('members', function($query) use ($userUloubi) {
            $query->where('user_id', $userUloubi->id)->where('status', 'active');
        })->get();
        
        $userUloubiPending = JointAccount::whereHas('members', function($query) use ($userUloubi) {
            $query->where('user_id', $userUloubi->id)->where('status', 'pending');
        })->get();
        
        echo "User uloubi7@gmail.com:\n";
        echo "  Active accounts: {$userUloubiActive->count()}\n";
        echo "  Pending invitations: {$userUloubiPending->count()}\n";
        
        foreach ($userUloubiPending as $account) {
            $member = $account->members->where('user_id', $userUloubi->id)->first();
            echo "    - {$account->account_name} (Role: {$member->role})\n";
        }
    }
    
    echo "\n=== Test Complete ===\n";
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
    echo "Stack trace:\n" . $e->getTraceAsString() . "\n";
} 