<?php
/**
 * TellerPro SaaS - Application Configuration
 */

// Load .env file if it exists
function loadEnv(): void {
    $envFile = dirname(__DIR__) . '/.env';
    if (file_exists($envFile)) {
        $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        foreach ($lines as $line) {
            // Skip comments
            if (str_starts_with(trim($line), '#')) {
                continue;
            }
            // Parse KEY=VALUE
            if (str_contains($line, '=')) {
                list($key, $value) = explode('=', $line, 2);
                $key = trim($key);
                $value = trim($value);
                // Remove quotes if present
                if (str_starts_with($value, '"') && str_ends_with($value, '"')) {
                    $value = substr($value, 1, -1);
                } elseif (str_starts_with($value, "'") && str_ends_with($value, "'")) {
                    $value = substr($value, 1, -1);
                }
                // Set environment variable
                putenv("$key=$value");
                $_ENV[$key] = $value;
                $_SERVER[$key] = $value;
            }
        }
    }
}

loadEnv();

define('APP_NAME',    getenv('APP_NAME') ?: 'TellerPro');
define('APP_VERSION', getenv('APP_VERSION') ?: '2.0.0');
define('APP_URL',     rtrim(getenv('APP_URL') ?: 'http://localhost/TellerPro', '/'));
define('APP_ROOT',    dirname(__DIR__));

// Database
define('DB_HOST',    getenv('DB_HOST') ?: 'localhost');
define('DB_NAME',    getenv('DB_NAME') ?: 'tellerpro');
define('DB_USER',    getenv('DB_USER') ?: 'root');
define('DB_PASS',    getenv('DB_PASS') ?: '');
define('DB_CHARSET', getenv('DB_CHARSET') ?: 'utf8mb4');

// Redis (for enterprise-scale caching)
define('REDIS_HOST',     getenv('REDIS_HOST') ?: '127.0.0.1');
define('REDIS_PORT',     getenv('REDIS_PORT') ?: 6379);
define('REDIS_PASSWORD', getenv('REDIS_PASSWORD') ?: null);
define('REDIS_ENABLED',  getenv('REDIS_ENABLED') === 'true');

// Session
define('SESSION_NAME',     'TELLERPRO_SESSION');
define('SUPERADMIN_SESSION','TELLERPRO_SA_SESSION');
define('SESSION_LIFETIME', 7200);
define('SESSION_HANDLER',   'files'); // 'files' or 'redis'
define('SESSION_PATH',     getenv('SESSION_PATH') ?: (APP_ROOT . '/storage/sessions'));
define('SESSION_GC_MAXLIFETIME', 14400); // 4 hours

// Storage Paths (configurable for shared hosting)
define('STORAGE_PATH',     getenv('STORAGE_PATH') ?: (APP_ROOT . '/storage'));
define('CACHE_PATH',       getenv('CACHE_PATH') ?: (STORAGE_PATH . '/cache'));
define('RATELIMIT_PATH',   getenv('RATELIMIT_PATH') ?: (STORAGE_PATH . '/ratelimit'));
define('LOGS_PATH',        getenv('LOGS_PATH') ?: (STORAGE_PATH . '/logs'));

// Security
define('CSRF_TOKEN_NAME',     'csrf_token');
define('MAX_LOGIN_ATTEMPTS',  5);
define('LOGIN_LOCKOUT_MINUTES', 15);

// Upload
define('UPLOAD_PATH',      APP_ROOT . '/uploads');
define('UPLOAD_URL',       APP_URL  . '/uploads');
define('MAX_UPLOAD_SIZE',  (int)(getenv('MAX_UPLOAD_SIZE') ?: (5 * 1024 * 1024)));
define('ALLOWED_IMAGE_TYPES', ['image/jpeg','image/png','image/gif','image/webp']);

// Pagination
define('ITEMS_PER_PAGE', 25);

// Trial period (days)
define('TRIAL_DAYS', (int)(getenv('TRIAL_DAYS') ?: 14));

// Environment
define('APP_ENV',   getenv('APP_ENV') ?: 'production');
define('APP_DEBUG', getenv('APP_DEBUG') === 'true' || APP_ENV === 'development');

if (APP_DEBUG) {
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
} else {
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
}

// Secure session settings (must be after APP_ENV is defined)
if (APP_ENV === 'production') {
    ini_set('session.cookie_httponly', 1);
    ini_set('session.cookie_secure', 1);
    ini_set('session.cookie_samesite', 'Strict');
    ini_set('session.use_strict_mode', 1);
} else {
    // Development settings
    ini_set('session.cookie_httponly', 1);
    ini_set('session.cookie_samesite', 'Lax');
    ini_set('session.use_strict_mode', 1);
}

date_default_timezone_set(getenv('TIMEZONE') ?: 'UTC');
