<?php
// Scratch script to test account direct deposit flag creation & updates

$baseUrl = 'http://localhost/projects/naveed_trader/api';

echo "1. Logging in as admin..." . PHP_EOL;
$loginResponse = apiCall('POST', $baseUrl . '/auth/login', [
    'username' => 'admin',
    'password' => 'admin123'
]);
$token = $loginResponse['data']['token'];

echo PHP_EOL . "2. Creating a new bank account with Direct Deposit ALLOWED..." . PHP_EOL;
$newAccountPayload = [
    'name' => 'Test MCB Bank',
    'type' => 'bank_cash',
    'sub_type' => 'bank',
    'opening_balance' => 10000,
    'balance_type' => 'Dr',
    'allow_direct_deposit' => 1
];

$createRes = apiCall('POST', $baseUrl . '/accounts', $newAccountPayload, $token);
$accountId = $createRes['data']['id'];
echo "Account Created ID: {$accountId}" . PHP_EOL;

// Fetch to verify value is 1
$accounts = apiCall('GET', $baseUrl . '/accounts?type=bank_cash', null, $token);
$createdAccount = null;
foreach ($accounts['data'] as $a) {
    if ($a['id'] == $accountId) {
        $createdAccount = $a;
        break;
    }
}

if (!$createdAccount) {
    echo "ERROR: Created account not found in list." . PHP_EOL;
    exit(1);
}

echo "Stored allow_direct_deposit value: {$createdAccount['allow_direct_deposit']}" . PHP_EOL;
if (parseIntVal($createdAccount['allow_direct_deposit']) === 1) {
    echo "SUCCESS: allow_direct_deposit is 1" . PHP_EOL;
} else {
    echo "ERROR: allow_direct_deposit is not 1!" . PHP_EOL;
    exit(1);
}

echo PHP_EOL . "3. Updating account to Direct Deposit NOT ALLOWED..." . PHP_EOL;
$updatePayload = [
    'allow_direct_deposit' => 0
];
apiCall('PUT', $baseUrl . "/accounts/{$accountId}", $updatePayload, $token);

// Fetch to verify value is 0
$accounts = apiCall('GET', $baseUrl . '/accounts?type=bank_cash', null, $token);
$updatedAccount = null;
foreach ($accounts['data'] as $a) {
    if ($a['id'] == $accountId) {
        $updatedAccount = $a;
        break;
    }
}

echo "Updated allow_direct_deposit value: {$updatedAccount['allow_direct_deposit']}" . PHP_EOL;
if (parseIntVal($updatedAccount['allow_direct_deposit']) === 0) {
    echo "SUCCESS: allow_direct_deposit is 0" . PHP_EOL;
} else {
    echo "ERROR: allow_direct_deposit is not 0!" . PHP_EOL;
    exit(1);
}

echo PHP_EOL . "4. Cleaning up (Deactivating/soft-deleting account)..." . PHP_EOL;
apiCall('DELETE', $baseUrl . "/accounts/{$accountId}", null, $token);
echo "SUCCESS: Cleanup completed." . PHP_EOL;

echo PHP_EOL . "=== ALL ACCOUNT FLAG API TESTS PASSED ===" . PHP_EOL;

function parseIntVal($val) {
    return is_null($val) ? 0 : intval($val);
}

function apiCall($method, $url, $data = null, $token = null) {
    $headers = ['Content-Type: application/json', 'Accept: application/json'];
    if ($token) $headers[] = 'Authorization: Bearer ' . $token;
    
    $opts = [
        'http' => [
            'method' => $method,
            'header' => implode("\r\n", $headers),
            'ignore_errors' => true
        ]
    ];
    if ($data) $opts['http']['content'] = json_encode($data);
    
    $result = file_get_contents($url, false, stream_context_create($opts));
    $decoded = json_decode($result, true);
    
    if (!$decoded || !$decoded['success']) {
        echo "ERROR: " . ($decoded['message'] ?? $result) . PHP_EOL;
        exit(1);
    }
    return $decoded;
}
