<?php
// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/test_errors.log');

header('Content-Type: application/json');

// Simple response to test if the endpoint is reachable
$response = [
    'success' => true,
    'message' => 'Test endpoint is working',
    'server' => [
        'php_version' => phpversion(),
        'server_software' => $_SERVER['SERVER_SOFTWARE'] ?? 'N/A',
        'request_method' => $_SERVER['REQUEST_METHOD'] ?? 'N/A'
    ]
];

try {
    // Test database connection
    require_once __DIR__ . '/config.php';
    require_once __DIR__ . '/db.php';
    
    // Test query
    $stmt = $pdo->query("SELECT 1 as test_value");
    $result = $stmt->fetch();
    
    $response['database'] = [
        'connected' => true,
        'test_query' => $result
    ];
    
    // Test user table
    $stmt = $pdo->query("SHOW TABLES LIKE 'users'");
    $usersTableExists = $stmt->rowCount() > 0;
    
    $response['tables'] = [
        'users_exists' => $usersTableExists
    ];
    
    if ($usersTableExists) {
        // Count users
        $stmt = $pdo->query("SELECT COUNT(*) as user_count FROM users");
        $count = $stmt->fetch();
        $response['tables']['user_count'] = $count['user_count'];
    }
    
} catch (Exception $e) {
    $response['success'] = false;
    $response['error'] = [
        'message' => $e->getMessage(),
        'file' => $e->getFile(),
        'line' => $e->getLine(),
        'trace' => $e->getTraceAsString()
    ];
}

echo json_encode($response, JSON_PRETTY_PRINT);
