<?php
// TEST FILE - Check if config files load correctly
// Place in: Triangle_Tree_Life_Number/api/test_config.php

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');

$errors = [];
$success = [];

// Test 1: Check if config files exist
if (file_exists('config/db.php')) {
    $success[] = '✅ config/db.php exists';
} else {
    $errors[] = '❌ config/db.php NOT FOUND';
}

if (file_exists('config/helpers.php')) {
    $success[] = '✅ config/helpers.php exists';
} else {
    $errors[] = '❌ config/helpers.php NOT FOUND';
}

// Test 2: Load config files
try {
    require_once 'config/db.php';
    $success[] = '✅ db.php loaded successfully';
} catch (Exception $e) {
    $errors[] = '❌ db.php error: ' . $e->getMessage();
}

try {
    require_once 'config/helpers.php';
    $success[] = '✅ helpers.php loaded successfully';
} catch (Exception $e) {
    $errors[] = '❌ helpers.php error: ' . $e->getMessage();
}

// Test 3: Check if $pdo is available
if (isset($pdo)) {
    $success[] = '✅ $pdo global variable is available';
    
    // Test 4: Try a simple query
    try {
        $stmt = $pdo->query("SELECT COUNT(*) as count FROM users");
        $result = $stmt->fetch();
        $success[] = '✅ Database query successful (' . $result['count'] . ' users found)';
    } catch (Exception $e) {
        $errors[] = '❌ Database query failed: ' . $e->getMessage();
    }
} else {
    $errors[] = '❌ $pdo global variable NOT available';
}

// Test 5: Check if verifyToken function exists
if (function_exists('verifyToken')) {
    $success[] = '✅ verifyToken() function exists';
} else {
    $errors[] = '❌ verifyToken() function NOT FOUND';
}

echo json_encode([
    'success' => $success,
    'errors' => $errors,
    'has_errors' => count($errors) > 0
], JSON_PRETTY_PRINT);
?>
