<?php
/**
 * Test Registration AJAX
 */

require_once '../app/Core/init.php';

echo "<h2>Registration Test</h2>";

// Test 1: Check if Security class has csrfField method
echo "<h3>Test 1: CSRF Token Generation</h3>";
try {
    $csrf = Security::csrfField();
    echo "✓ CSRF Token Generated<br>";
    echo "<pre>" . htmlspecialchars($csrf) . "</pre>";
} catch (Exception $e) {
    echo "✗ Error: " . $e->getMessage() . "<br>";
}

// Test 2: Check if models exist
echo "<h3>Test 2: Model Classes</h3>";
$classes = ['User', 'Customer', 'Vendor', 'Wallet'];
foreach ($classes as $class) {
    if (class_exists($class)) {
        echo "✓ $class class exists<br>";
    } else {
        echo "✗ $class class NOT found<br>";
    }
}

// Test 3: Check if Mailer works
echo "<h3>Test 3: Mailer Class</h3>";
try {
    $mailer = new Mailer();
    echo "✓ Mailer class instantiated<br>";
} catch (Exception $e) {
    echo "✗ Mailer error: " . $e->getMessage() . "<br>";
}

// Test 4: Simulate AJAX request
echo "<h3>Test 4: Simulate Customer Registration</h3>";
echo "<form method='POST' action='register.php'>";
echo Security::csrfField();
echo "<input type='hidden' name='ajax' value='1'>";
echo "<input type='hidden' name='user_type' value='customer'>";
echo "First Name: <input type='text' name='first_name' value='Test'><br>";
echo "Last Name: <input type='text' name='last_name' value='User'><br>";
echo "Email: <input type='email' name='email' value='test" . time() . "@test.com'><br>";
echo "Phone: <input type='tel' name='phone' value='08012345678'><br>";
echo "Password: <input type='password' name='password' value='Test1234'><br>";
echo "Confirm: <input type='password' name='confirm_password' value='Test1234'><br>";
echo "<input type='checkbox' name='agree_terms' value='1' checked> Agree<br>";
echo "<button type='submit'>Test Submit</button>";
echo "</form>";

echo "<hr>";
echo "<h3>Test 5: Check Error Logs</h3>";
$log_file = ROOT_PATH . '/storage/logs/app.log';
if (file_exists($log_file)) {
    $logs = file_get_contents($log_file);
    $recent_logs = substr($logs, -2000); // Last 2000 chars
    echo "<pre style='background: #f5f5f5; padding: 10px; max-height: 300px; overflow-y: scroll;'>";
    echo htmlspecialchars($recent_logs);
    echo "</pre>";
} else {
    echo "Log file not found at: $log_file";
}