<?php
// Test script for sign-in flow
$base_url = 'http://localhost/cmsb/apiv11.php'; // Change this to your actual URL

// Test 1: Send OTP
echo "Test 1: Sending OTP\n";
$ch = curl_init($base_url . '?send-otp');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['email' => 'test@example.com']));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$response = curl_exec($ch);
$otp_response = json_decode($response, true);
echo "Response: " . $response . "\n\n";

if (!$otp_response['success']) {
    die("Failed to send OTP\n");
}

// Get OTP from response (for testing only)
$otp = $otp_response['debug_otp'];

// Test 2: Verify OTP
echo "Test 2: Verifying OTP\n";
$ch = curl_init($base_url . '?verify-otp');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'email' => 'test@example.com',
    'otp' => $otp
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$response = curl_exec($ch);
$verify_response = json_decode($response, true);
echo "Response: " . $response . "\n\n";

if (!$verify_response['success']) {
    die("Failed to verify OTP\n");
}

// If user doesn't exist, complete profile
if ($verify_response['redirect'] === 'complete-profile') {
    echo "Test 3: Completing Profile\n";
    $ch = curl_init($base_url . '?complete-profile');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
        'email' => 'test@example.com',
        'full_name' => 'Test User',
        'phone_number' => '03001234567'
    ]));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    $response = curl_exec($ch);
    $profile_response = json_decode($response, true);
    echo "Response: " . $response . "\n\n";

    if (!$profile_response['success']) {
        die("Failed to complete profile\n");
    }
}

echo "All tests completed successfully!\n"; 