<?php
/**
 * API Test Page
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/../config/config.php';

if (!isLoggedIn()) {
    die('Please login first');
}

$pdo = getDBConnection();
?>

<!DOCTYPE html>
<html>
<head>
    <title>API Test</title>
    <style>
        body { font-family: Arial; padding: 20px; }
        .success { color: green; }
        .error { color: red; }
        pre { background: #f4f4f4; padding: 10px; border-radius: 5px; }
        button { padding: 10px; margin: 5px; cursor: pointer; }
    </style>
</head>
<body>
    <h1>API Test Page</h1>
    
    <h2>Test Database Connection</h2>
    <?php if ($pdo): ?>
        <p class="success">✅ Database connected successfully</p>
    <?php else: ?>
        <p class="error">❌ Database connection failed</p>
    <?php endif; ?>
    
    <h2>Test States</h2>
    <?php
    $states = $pdo->query("SELECT COUNT(*) as count FROM states")->fetch();
    echo "<p>Total states in database: " . $states['count'] . "</p>";
    ?>
    
    <h2>Test LGAs</h2>
    <?php
    $lgas = $pdo->query("SELECT COUNT(*) as count FROM lgas")->fetch();
    echo "<p>Total LGAs in database: " . $lgas['count'] . "</p>";
    ?>
    
    <h2>Test Zones</h2>
    <?php
    $zones = $pdo->query("SELECT COUNT(*) as count FROM zones")->fetch();
    echo "<p>Total zones in database: " . $zones['count'] . "</p>";
    ?>
    
    <h2>Test Commands</h2>
    <?php
    $commands = $pdo->query("SELECT COUNT(*) as count FROM commands")->fetch();
    echo "<p>Total commands in database: " . $commands['count'] . "</p>";
    ?>
    
    <h2>Test API Endpoints</h2>
    
    <div>
        <h3>Test get_lgas.php</h3>
        <button onclick="testLGAs(25)">Test Lagos State (ID: 25)</button>
        <button onclick="testLGAs(16)">Test FCT (ID: 16)</button>
        <pre id="lgasResult">Click button to test</pre>
    </div>
    
    <div>
        <h3>Test get_commands.php</h3>
        <button onclick="testCommands(1)">Test Zone A (ID: 1)</button>
        <button onclick="testCommands(2)">Test Zone B (ID: 2)</button>
        <pre id="commandsResult">Click button to test</pre>
    </div>
    
    <script>
        async function testLGAs(stateId) {
            const result = document.getElementById('lgasResult');
            result.textContent = 'Loading...';
            
            try {
                const response = await fetch('get_lgas.php?state_id=' + stateId);
                const data = await response.json();
                result.textContent = JSON.stringify(data, null, 2);
            } catch (error) {
                result.textContent = 'Error: ' + error.message;
            }
        }
        
        async function testCommands(zoneId) {
            const result = document.getElementById('commandsResult');
            result.textContent = 'Loading...';
            
            try {
                const response = await fetch('get_commands.php?zone_id=' + zoneId);
                const data = await response.json();
                result.textContent = JSON.stringify(data, null, 2);
            } catch (error) {
                result.textContent = 'Error: ' + error.message;
            }
        }
    </script>
</body>
</html>