<?php
/**
 * DIAGNOSTIC: test DB + file loading
 * Visit: http://localhost/DreamNew12/User/ajaxCalls/testConnection.php
 * DELETE this file after debugging!
 */
header('Content-Type: application/json');
ob_start();

$results = [];

// Test 1: File paths
$base = dirname(__DIR__, 2);
$results['base_path']   = $base;
$results['db_exists']   = file_exists($base . '/connection/db.php');
$results['eng_exists']  = file_exists($base . '/connection/MatrixEngine.php');

// Test 2: Load db.php
try {
    require_once $base . '/connection/db.php';
    $results['db_php_loaded'] = true;
} catch (Throwable $e) {
    $results['db_php_error'] = $e->getMessage();
    echo json_encode($results); exit;
}

// Test 3: DB connection
try {
    $db = getDB();
    $results['db_connected'] = true;
    $results['db_name'] = DB_NAME;
} catch (Throwable $e) {
    $results['db_error'] = $e->getMessage();
    $results['fix'] = 'Edit DB_NAME, DB_USER, DB_PASS in connection/db.php';
    echo json_encode($results); exit;
}

// Test 4: Check tables
$tables = [
    'meddolic_user_details',
    'meddolic_config_pool_list',
    'meddolic_config_misc_setting',
    'gm_package_purchases',
    'gm_pending_reg',
    'gm_auto_upgrades',
];
$results['tables'] = [];
foreach ($tables as $t) {
    try {
        $db->query("SELECT 1 FROM `$t` LIMIT 1");
        $results['tables'][$t] = '✓ exists';
    } catch (Throwable $e) {
        $results['tables'][$t] = '✗ MISSING — run schema_new.sql';
    }
}

// Test 5: Load MatrixEngine
try {
    require_once $base . '/connection/MatrixEngine.php';
    $eng = new MatrixEngine();
    $results['engine_loaded'] = true;
} catch (Throwable $e) {
    $results['engine_error'] = $e->getMessage();
}

ob_end_clean();
echo json_encode($results, JSON_PRETTY_PRINT);
