<?php
declare(strict_types=1);

/**
 * Loads .env and exposes configuration via the env() helper and a small set of
 * top-level constants. Called once by public/index.php and by bin/console.
 */

$envPath = ROOT;
if (file_exists($envPath . '/.env')) {
    \Dotenv\Dotenv::createImmutable($envPath)->load();
} else {
    \Dotenv\Dotenv::createImmutable($envPath, '.env.example')->load();
    error_log('[orchview2] .env missing — falling back to .env.example. Copy and edit it.');
}

date_default_timezone_set(env('APP_TIMEZONE', 'Africa/Dar_es_Salaam'));

define('APP_ENV',   env('APP_ENV', 'production'));
define('APP_DEBUG', env('APP_DEBUG', 'false') === 'true');
define('APP_NAME',  env('APP_NAME', 'OrchView 2'));
define('APP_BRAND', env('APP_BRAND', 'ROOKASYSTEMS'));
define('PROOT',     rtrim((string) env('PROOT', ''), '/'));
define('STORAGE',   ROOT . DS . 'storage');

if (APP_DEBUG) {
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
} else {
    error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
    ini_set('display_errors', '0');
    ini_set('log_errors', '1');
    ini_set('error_log', STORAGE . DS . 'logs' . DS . 'php-error.log');
}

ini_set('session.save_path',        STORAGE . DS . 'sessions');
ini_set('session.cookie_httponly',  '1');
ini_set('session.use_strict_mode',  '1');
ini_set('session.cookie_samesite',  'Lax');
if (!empty($_SERVER['HTTPS'])) {
    ini_set('session.cookie_secure', '1');
}
session_name(env('SESSION_NAME', 'ov2'));
