<?php
/*
 * ============================================================
 * TryTrack — Database Connection
 * ============================================================
 *
 * SECURITY: Never hardcode credentials in source files.
 * Set these environment variables on your server:
 *
 *   export DB_HOST="localhost"
 *   export DB_USER="your_db_user"
 *   export DB_PASS="your_db_password"
 *   export DB_NAME="your_db_name"
 *
 * In cPanel / hosting control panels, use the "Environment
 * Variables" section or add them to your .htaccess:
 *   SetEnv DB_USER "your_db_user"
 *   SetEnv DB_PASS "your_db_password"
 *   SetEnv DB_NAME "your_db_name"
 *
 * ============================================================
 */

$host = getenv("DB_HOST") ?: "localhost";
$user = getenv("DB_USER") ?: "";
$pass = getenv("DB_PASS") ?: "";
$db   = getenv("DB_NAME") ?: "";

if (!$user || !$db) {
    error_log("TryTrack: DB_USER or DB_NAME environment variable not set.");
    http_response_code(500);
    die(json_encode(["error" => "Database configuration error."]));
}

$conn = new mysqli($host, $user, $pass, $db);

if ($conn->connect_error) {
    error_log("TryTrack DB connection failed: " . $conn->connect_error);
    http_response_code(500);
    die(json_encode(["error" => "Database connection failed."]));
}

$conn->set_charset("utf8mb4");
