<?php

// === DATABASE CONNECTION ===
require_once 'includes/config.php';

mysqli_report(MYSQLI_REPORT_OFF);

$dbHost = defined('DB_HOST') ? DB_HOST : '127.0.0.1';
$dbUser = defined('DB_USER') ? DB_USER : 'root';
$dbPass = defined('DB_PASS') ? DB_PASS : '';
$dbName = defined('DB_NAME') ? DB_NAME : '';
$dbPort = defined('DB_PORT') ? (int) DB_PORT : 3306;

$conn = @new mysqli($dbHost, $dbUser, $dbPass, $dbName, $dbPort);

if ($conn->connect_errno) {
    http_response_code(500);
    $safeDetails = htmlspecialchars($conn->connect_error ?: 'Unknown database connection error.');
    $safeHost = htmlspecialchars($dbHost);
    $safeDb = htmlspecialchars($dbName);
    $safePort = htmlspecialchars((string) $dbPort);

    die(
        '<div style="font-family: Inter, Arial, sans-serif; max-width: 760px; margin: 48px auto; padding: 24px; border: 1px solid #e5e7eb; border-radius: 16px; background: #fff;">'
        . '<h2 style="margin:0 0 12px; color:#111827;">Database connection failed</h2>'
        . '<p style="margin:0 0 10px; color:#374151;">Unable to connect to MySQL for database <strong>' . $safeDb . '</strong> on <strong>' . $safeHost . ':' . $safePort . '</strong>.</p>'
        . '<p style="margin:0 0 16px; color:#b91c1c;">' . $safeDetails . '</p>'
        . '<div style="color:#4b5563; line-height:1.6;">'
        . '<div>Check these items in XAMPP:</div>'
        . '<div>1. Start MySQL in the XAMPP Control Panel.</div>'
        . '<div>2. Confirm the database <strong>' . $safeDb . '</strong> exists in phpMyAdmin.</div>'
        . '<div>3. If MySQL uses a different port, update <code>includes/config.php</code>.</div>'
        . '</div>'
        . '</div>'
    );
}

$conn->set_charset("utf8mb4");

// === COUNTRIES / STATES DATASET ===
// This project expects full `countries` and `states` tables (imported via SQL dump).
// The previous `india_locations` seed was removed to avoid conflicts with full datasets.

// NOTE: Create tables via SQL in phpMyAdmin. Avoid auto-creating tables here.

?>
