<?php
/**
 * S.P.M Dashboard - Database Configuration
 * Sektor Pembangunan Murid
 */

define('DB_HOST', 'localhost');
define('DB_NAME', 'alhafizx_dashboardspm');
define('DB_USER', 'alhafizx_dashboardspm');
define('DB_PASS', 'Dashboard@spm');
define('DB_CHARSET', 'utf8mb4');

define('APP_NAME', 'S.P.M Dashboard');
define('APP_TAGLINE', 'Sektor Pembangunan Murid');
// Auto-detect BASE_URL for both localhost and production server
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)) ? "https://" : "http://";
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';

if ($host === 'localhost' || $host === '127.0.0.1') {
    $folder = basename(dirname(__DIR__));
    define('BASE_URL', $protocol . $host . '/' . $folder . '/');
} else {
    define('BASE_URL', $protocol . $host . '/');
}
define('UPLOAD_PATH', __DIR__ . '/../uploads/');
define('UPLOAD_URL', BASE_URL . 'uploads/');

try {
    $dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=" . DB_CHARSET;
    $options = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci"
    ];
    $pdo = new PDO($dsn, DB_USER, DB_PASS, $options);
} catch (PDOException $e) {
    http_response_code(500);
    die(json_encode(['error' => 'Sambungan database gagal: ' . $e->getMessage()]));
}
