<?php

$env = [];
$envFile = __DIR__ . '/../.env';

if (file_exists($envFile)) {
    $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    
    foreach ($lines as $line) {
        if (strpos(trim($line), '#') === 0) {
            continue;
        }
        
        list($key, $value) = explode('=', $line, 2);
        $env[trim($key)] = trim($value);
    }
}

foreach ($env as $key => $value) {
    putenv("$key=$value");
    $_ENV[$key] = $value;
    $_SERVER[$key] = $value;
}

define('APP_ENV', getenv('APP_ENV') ?: 'local');
define('SITE_URL', getenv('SITE_URL') ?: 'http://localhost/ibiza');

if (APP_ENV === 'local') {
    define('DB_HOST', getenv('LOCAL_DB_HOST') ?: 'localhost');
    define('DB_PORT', getenv('LOCAL_DB_PORT') ?: '3306');
    define('DB_USER', getenv('LOCAL_DB_USER') ?: 'root');
    define('DB_PASS', getenv('LOCAL_DB_PASS') ?: '');
    define('DB_NAME', getenv('LOCAL_DB_NAME') ?: 'ibiza_db');
} else {
    define('DB_HOST', getenv('SERVER_DB_HOST') ?: 'localhost');
    define('DB_PORT', getenv('SERVER_DB_PORT') ?: '3306');
    define('DB_USER', getenv('SERVER_DB_USER') ?: 'root');
    define('DB_PASS', getenv('SERVER_DB_PASS') ?: '');
    define('DB_NAME', getenv('SERVER_DB_NAME') ?: 'ibiza_db');
}

define('SESSION_TIMEOUT', (int)(getenv('SESSION_TIMEOUT') ?: 28800));
define('JWT_SECRET', getenv('JWT_SECRET') ?: 'ibiza_jwt_secret_key_CHANGE_IN_PRODUCTION_2025!@#');
define('JWT_EXPIRATION', (int)(getenv('JWT_EXPIRATION') ?: 86400)); // 24 horas

define('PDF_PAGE_ORIENTATION', 'L');
define('PDF_PAGE_FORMAT', 'A3');
define('PDF_UNIT', 'mm');
