<?php
// ============================================================
//  MJPOS – Configuration
// ============================================================
define('APP_NAME',    'MJPOS');
define('APP_VERSION', '1.1.0');

// ── APP_URL: auto-detect scheme (http/https) + host + base path ──
$_isHttps = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
         || (($_SERVER['SERVER_PORT'] ?? 80) == 443)
         || (($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '') === 'https');

$_scheme = $_isHttps ? 'https' : 'http';
$_host   = $_SERVER['HTTP_HOST'] ?? 'localhost';

// Base path = directory containing this script, minus /index.php
$_script = $_SERVER['SCRIPT_NAME'] ?? '/mjpos/public/index.php';
$_base   = rtrim(str_replace('\\', '/', dirname($_script)), '/');
if ($_base === '' || $_base === '.') { $_base = ''; }

define('APP_URL', $_scheme . '://' . $_host . $_base);

// ── Paths ─────────────────────────────────────────────────────
define('BASE_PATH',    dirname(__DIR__));
define('APP_PATH',     BASE_PATH . '/app');
define('PUBLIC_PATH',  BASE_PATH . '/public');
define('STORAGE_PATH', BASE_PATH . '/storage');
define('BACKUP_PATH',  STORAGE_PATH . '/backups');
define('UPLOAD_PATH',  PUBLIC_PATH . '/uploads');
define('UPLOAD_URL',   APP_URL    . '/uploads');
define('LOG_PATH',     STORAGE_PATH . '/logs');

// ── Database ─────────────────────────────────────────────────
define('DB_HOST',    'localhost');
define('DB_NAME',    'mjposliv_mjpos_db_kussiya');
define('DB_USER',    'mjposliv_kussiya');
define('DB_PASS',    'Ishara@1994Bubu');
define('DB_CHARSET', 'utf8mb4');

// ── Session ───────────────────────────────────────────────────
define('SESSION_NAME',     'mjpos_sess');
define('SESSION_LIFETIME', 7200);

// ── Security ──────────────────────────────────────────────────
define('CSRF_TOKEN_NAME',  '_mjpos_token');
define('PASSWORD_ALGO',    PASSWORD_BCRYPT);
define('PASSWORD_OPTIONS', ['cost' => 12]);
define('MAX_LOGIN_ATTEMPTS', 5);
define('LOGIN_LOCKOUT_TIME', 900);
define('LOGIN_WINDOW',       300);

// ── Upload limits ─────────────────────────────────────────────
define('MAX_FILE_SIZE',     2 * 1024 * 1024);
define('ALLOWED_IMG_TYPES', ['image/jpeg','image/png','image/webp','image/gif']);
define('ALLOWED_IMG_EXTS',  ['jpg','jpeg','png','webp','gif']);

// ── Pagination ────────────────────────────────────────────────
define('PER_PAGE', 25);

// ── Date/Time ─────────────────────────────────────────────────
define('TIMEZONE', 'Asia/Colombo');
date_default_timezone_set(TIMEZONE);

// ── App Timezone alias ────────────────────────────────────────
if (!defined('APP_TIMEZONE')) define('APP_TIMEZONE', TIMEZONE);

// ── Environment ───────────────────────────────────────────────
define('APP_ENV',   'production');
define('APP_DEBUG', false);

error_reporting(0);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
if (!is_dir(LOG_PATH)) @mkdir(LOG_PATH, 0755, true);
ini_set('error_log', LOG_PATH . '/php_errors.log');

// ── Autoloader ────────────────────────────────────────────────
spl_autoload_register(function (string $class): void {
    $paths = [
        APP_PATH . '/core/',
        APP_PATH . '/controllers/',
        APP_PATH . '/models/',
        APP_PATH . '/services/',
    ];
    foreach ($paths as $path) {
        $file = $path . $class . '.php';
        if (file_exists($file)) {
            require_once $file;
            return;
        }
    }
    error_log("MJPOS Autoloader: class '{$class}' not found in " . APP_PATH);
});