<?php
// Supernova Configuration File

// Local secrets override (gitignored). Define DB_* / RAZORPAY_* there on real
// servers; the defaults below only apply when a constant is not yet defined.
if (file_exists(__DIR__ . '/config.local.php')) {
    require __DIR__ . '/config.local.php';
}

// Database Configuration
defined('DB_HOST') || define('DB_HOST', 'localhost');
defined('DB_USER') || define('DB_USER', 'root');
defined('DB_PASS') || define('DB_PASS', '');
defined('DB_NAME') || define('DB_NAME', 'supernova_db');

// Application Configuration - Dynamic URL resolution
if (!defined('APP_URL')) {
    if (isset($_SERVER['HTTP_HOST'])) {
        $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? 'https' : 'http';
        $docRoot = isset($_SERVER['DOCUMENT_ROOT']) ? str_replace('\\', '/', realpath($_SERVER['DOCUMENT_ROOT'])) : '';
        $appRoot = str_replace('\\', '/', realpath(__DIR__ . '/..'));
        $appSubPath = '';
        if (!empty($docRoot) && !empty($appRoot) && strpos($appRoot, $docRoot) === 0) {
            $appSubPath = substr($appRoot, strlen($docRoot));
        } else {
            $appSubPath = '/supernova';
        }
        define('APP_URL', rtrim($protocol . '://' . $_SERVER['HTTP_HOST'] . $appSubPath, '/'));
    } else {
        define('APP_URL', 'http://localhost/supernova');
    }
}
define('APP_NAME', 'Supernova 2.0');

// Registration Links (ERP Portal)
define('ERP_GLOBAL_REGISTRATION_URL', 'https://erp.mgmu.ac.in/asd_EventPublicUserMaster.htm?eventID=241');
define('ERP_JUNIOR_SHARK_REGISTRATION_URL', 'https://erp.mgmu.ac.in/asd_EventPublicUserMaster.htm?eventID=242');

// Razorpay Configuration (test keys — real keys belong in config.local.php)
defined('RAZORPAY_KEY_ID') || define('RAZORPAY_KEY_ID', 'rzp_test_TMAjNxHQYI7Mty');
defined('RAZORPAY_KEY_SECRET') || define('RAZORPAY_KEY_SECRET', 'gkJYQTDkJZoHhVuKXxxoqJI2');

// Error reporting: verbose on localhost only; production logs instead of displaying
$snIsLocal = in_array($_SERVER['REMOTE_ADDR'] ?? '', ['127.0.0.1', '::1'], true);
error_reporting(E_ALL);
ini_set('display_errors', $snIsLocal ? '1' : '0');
ini_set('display_startup_errors', $snIsLocal ? '1' : '0');
ini_set('log_errors', '1');

// Session Setup — hardened cookies (must run before session_start)
if (session_status() === PHP_SESSION_NONE) {
    $snIsHttps = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on');
    session_set_cookie_params([
        'lifetime' => 0,
        'path'     => '/',
        'secure'   => $snIsHttps,
        'httponly' => true,
        'samesite' => 'Lax',
    ]);
    ini_set('session.use_strict_mode', '1');
    session_start();
}

// Security core: headers + CSRF + login throttling (needs the session above)
require_once __DIR__ . '/security.php';
send_security_headers();

