<?php
$localConfig = [];
$localFile = __DIR__ . '/config.local.php';
if (is_file($localFile)) {
    $loaded = require $localFile;
    if (is_array($loaded)) {
        $localConfig = $loaded;
    }
}

$env = static function (string $key, string $default = '') use ($localConfig): string {
    $value = getenv($key);
    if ($value !== false && $value !== '') {
        return (string) $value;
    }
    if (array_key_exists($key, $localConfig) && $localConfig[$key] !== '') {
        return (string) $localConfig[$key];
    }
    return $default;
};

$envBool = static function (string $key, bool $default = false) use ($env): bool {
    $fallback = $default ? '1' : '0';
    return filter_var($env($key, $fallback), FILTER_VALIDATE_BOOL);
};

$envInt = static function (string $key, int $default, int $minimum = 0) use ($env): int {
    return max($minimum, (int) $env($key, (string) $default));
};

define('APP_NAME', $env('APP_NAME', 'Purchase Haat'));
define('BRAND_NAME', $env('BRAND_NAME', 'Smart Tech OMP'));
define('APP_ENV', strtolower($env('APP_ENV', 'production')));
define('APP_DEBUG', $envBool('APP_DEBUG', APP_ENV !== 'production'));
define('APP_URL', rtrim($env('APP_URL', ''), '/'));
define('APP_KEY', $env('APP_KEY', ''));
define('FORCE_HTTPS', $envBool('FORCE_HTTPS', false));
define('TRUST_PROXY_HEADERS', $envBool('TRUST_PROXY_HEADERS', false));

define('DB_HOST', $env('DB_HOST', 'localhost'));
define('DB_PORT', $envInt('DB_PORT', 3306, 1));
define('DB_NAME', $env('DB_NAME', ''));
define('DB_USER', $env('DB_USER', ''));
define('DB_PASS', $env('DB_PASS', ''));
define('DB_CHARSET', 'utf8mb4');

define('SESSION_NAME', preg_replace('/[^a-zA-Z0-9_-]/', '', $env('SESSION_NAME', 'ph_smarttech_session')) ?: 'ph_smarttech_session');
define('SESSION_IDLE_TIMEOUT', $envInt('SESSION_IDLE_TIMEOUT', 1800, 300));
define('SESSION_ABSOLUTE_TIMEOUT', $envInt('SESSION_ABSOLUTE_TIMEOUT', 28800, 900));
define('SESSION_REGENERATE_INTERVAL', $envInt('SESSION_REGENERATE_INTERVAL', 900, 60));
define('SESSION_STATUS_RECHECK_INTERVAL', $envInt('SESSION_STATUS_RECHECK_INTERVAL', 120, 30));
define('CSRF_KEY', 'purchase_haat_csrf_token');

define('APP_CURRENCY_SYMBOL', '৳');
define('DEFAULT_TIMEZONE', $env('APP_TIMEZONE', 'Asia/Dhaka'));
define('WITHDRAWAL_CHARGE_PERCENT', (float) $env('WITHDRAWAL_CHARGE_PERCENT', '7.5'));
define('MAX_UPLOAD_BYTES', $envInt('MAX_UPLOAD_BYTES', 5 * 1024 * 1024, 1024));
define('MAX_IMAGE_PIXELS', $envInt('MAX_IMAGE_PIXELS', 24000000, 1000000));
define('PASSWORD_MIN_LENGTH', $envInt('PASSWORD_MIN_LENGTH', 8, 8));

define('PAYMENT_SANDBOX', $envBool('PAYMENT_SANDBOX', true));
define('SSLCOMMERZ_STORE_ID', $env('SSLCOMMERZ_STORE_ID'));
define('SSLCOMMERZ_STORE_PASSWORD', $env('SSLCOMMERZ_STORE_PASSWORD'));
define('BKASH_APP_KEY', $env('BKASH_APP_KEY'));
define('BKASH_APP_SECRET', $env('BKASH_APP_SECRET'));
define('NAGAD_MERCHANT_ID', $env('NAGAD_MERCHANT_ID'));
define('NAGAD_MERCHANT_KEY', $env('NAGAD_MERCHANT_KEY'));

define('MAIL_FROM_ADDRESS', $env('MAIL_FROM_ADDRESS', ''));
define('MAIL_FROM_NAME', $env('MAIL_FROM_NAME', BRAND_NAME));
define('CRON_TOKEN', $env('CRON_TOKEN'));
define('BACKUP_RETENTION_DAYS', $envInt('BACKUP_RETENTION_DAYS', 14, 1));

date_default_timezone_set(DEFAULT_TIMEZONE);
