<?php
// backend-php/public/test_db.php
// Debug script to test Database Connection

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

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

require_once __DIR__ . '/../src/Utils/Env.php';
require_once __DIR__ . '/../src/Config/Database.php';

use App\Utils\Env;
use App\Config\Database;

// Load Env
Env::load(__DIR__ . '/../../.env');

$response = [
    'status' => 'pending',
    'env_host' => Env::get('DB_HOST'),
    'env_user' => Env::get('DB_USER'),
    'env_db' => Env::get('DB_NAME'),
    'env_path_check' => [
        'path' => __DIR__ . '/../../.env',
        'exists' => file_exists(__DIR__ . '/../../.env') ? 'YES' : 'NO',
        'realpath' => realpath(__DIR__ . '/../../.env')
    ],
    'message' => ''
];

try {
    $db = Database::getInstance()->getConnection();
    $response['status'] = 'success';
    $response['message'] = 'Database connection successful!';
    $response['pdo_attributes'] = [
        'server_info' => $db->getAttribute(PDO::ATTR_SERVER_INFO),
        'driver_name' => $db->getAttribute(PDO::ATTR_DRIVER_NAME)
    ];
} catch (Exception $e) {
    $response['status'] = 'error';
    $response['message'] = $e->getMessage();
    $response['trace'] = $e->getTraceAsString();
}

echo json_encode($response);
