<?php
// Test API Connection
require_once 'config.php';

echo "<h1>ERP System - Test de Conexión API</h1>";

try {
    // Test Database Connection
    $db = Database::getInstance()->getConnection();
    echo "<p style='color: green;'>✅ Conexión a base de datos exitosa</p>";
    
    // Test Tables
    $tables = ['empresas', 'clientes', 'productos', 'facturas', 'usuarios'];
    foreach ($tables as $table) {
        $stmt = $db->prepare("SELECT COUNT(*) as count FROM $table LIMIT 1");
        $stmt->execute();
        echo "<p style='color: green;'>✅ Tabla '$table' accesible</p>";
    }
    
    // Test Sample Data
    $stmt = $db->prepare("SELECT COUNT(*) as empresas FROM empresas WHERE activa = TRUE");
    $stmt->execute();
    $empresas = $stmt->fetch();
    echo "<p style='color: blue;'>📊 Empresas activas: " . $empresas['empresas'] . "</p>";
    
    $stmt = $db->prepare("SELECT COUNT(*) as clientes FROM clientes WHERE activo = TRUE");
    $stmt->execute();
    $clientes = $stmt->fetch();
    echo "<p style='color: blue;'>📊 Clientes activos: " . $clientes['clientes'] . "</p>";
    
    $stmt = $db->prepare("SELECT COUNT(*) as productos FROM productos WHERE activo = TRUE");
    $stmt->execute();
    $productos = $stmt->fetch();
    echo "<p style='color: blue;'>📊 Productos activos: " . $productos['productos'] . "</p>";
    
    // Test API Endpoints
    echo "<h2>Test de Endpoints API</h2>";
    
    $base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]/prub";
    
    $endpoints = [
        'info' => 'GET',
        'empresas' => 'GET',
        'dashboard?empresa_id=1' => 'GET'
    ];
    
    foreach ($endpoints as $endpoint => $method) {
        $url = "$base_url/$endpoint";
        echo "<p>🔗 Testing: $method $url</p>";
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        
        if ($method === 'POST') {
            curl_setopt($ch, CURLOPT_POST, true);
        }
        
        $response = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        
        if ($http_code === 200) {
            echo "<p style='color: green;'>✅ $endpoint - OK (HTTP $http_code)</p>";
        } else {
            echo "<p style='color: red;'>❌ $endpoint - Error (HTTP $http_code)</p>";
            echo "<p style='color: orange;'>Response: " . substr($response, 0, 200) . "...</p>";
        }
    }
    
    echo "<h2>Configuración del Sistema</h2>";
    echo "<p>📁 Directorio API: " . __DIR__ . "</p>";
    echo "<p>🗄️ Base de datos: " . DB_NAME . "</p>";
    echo "<p>🌐 Charset: " . DB_CHARSET . "</p>";
    echo "<p>⏰ Timezone: " . TIMEZONE . "</p>";
    echo "<p>📅 Fecha actual: " . date('Y-m-d H:i:s') . "</p>";
    
    echo "<h2>Prueba de Login</h2>";
    echo "<form method='post' action='usuarios.php'>";
    echo "<input type='hidden' name='method' value='POST'>";
    echo "<label>Username: <input type='text' name='username' value='admin' required></label><br><br>";
    echo "<label>Password: <input type='password' name='password' value='admin123' required></label><br><br>";
    echo "<input type='submit' value='Test Login'>";
    echo "</form>";
    
} catch (Exception $e) {
    echo "<p style='color: red;'>❌ Error: " . $e->getMessage() . "</p>";
}

echo "<hr>";
echo "<p><a href='../index.php'>← Volver al inicio</a></p>";
?>