<?php
defined('BASEPATH') or exit('No direct script access allowed');

/**
 * Try to load .env into a local array. Falls back to hardcoded values below.
 */
$_ENV_LOCAL = [];
$_envPath   = __DIR__ . '/../../.env';
if (@is_readable($_envPath)) {
    $_raw = @file_get_contents($_envPath);
    if (is_string($_raw) && $_raw !== '') {
        if (substr($_raw, 0, 3) === "\xEF\xBB\xBF") $_raw = substr($_raw, 3);
        $_raw = str_replace(["\r\n", "\r"], "\n", $_raw);
        foreach (explode("\n", $_raw) as $_line) {
            $_line = trim($_line);
            if ($_line === '' || $_line[0] === '#' || strpos($_line, '=') === false) continue;
            [$_key, $_val] = explode('=', $_line, 2);
            $_key = trim($_key);
            $_val = trim($_val, " \t\"'");
            if ($_key !== '') $_ENV_LOCAL[$_key] = $_val;
        }
        unset($_raw, $_line, $_key, $_val);
    }
}
unset($_envPath);

if (!function_exists('_env')) {
    function _env($key, $default = null) {
        global $_ENV_LOCAL;
        if (isset($_ENV_LOCAL[$key]) && $_ENV_LOCAL[$key] !== '') return $_ENV_LOCAL[$key];
        $v = getenv($key);
        if ($v !== false && $v !== '') return $v;
        if (isset($_ENV[$key])    && $_ENV[$key]    !== '') return $_ENV[$key];
        if (isset($_SERVER[$key]) && $_SERVER[$key] !== '') return $_SERVER[$key];
        return $default;
    }
}

/**
 * Values: prefer .env / environment, fall back to production hardcoded defaults.
 */
define('APP_BASE_URL',    _env('APP_BASE_URL',  'https://ticket-manager.demosystem.in/'));
define('APP_ENC_KEY',     '26f806b868d65016d9b1d9e813a9f131');
define('APP_DB_HOSTNAME', _env('DB_HOSTNAME',   'localhost'));
define('APP_DB_USERNAME', _env('DB_USERNAME',   'demosystem_ticket_manager'));
define('APP_DB_PASSWORD', _env('DB_PASSWORD',   'S8~A7A0ZgFs@&58o'));
define('APP_DB_NAME',     _env('DB_DATABASE',   'demosystem_ticket_manager'));

define('APP_DB_CHARSET',              'utf8mb4');
define('APP_DB_COLLATION',            'utf8mb4_unicode_ci');
define('SESS_DRIVER',                 'database');
define('SESS_SAVE_PATH',              'sessions');
define('APP_SESSION_COOKIE_SAME_SITE','Lax');
define('APP_CSRF_PROTECTION',         true);
