<?php
/**
 * Zabibu Residence - Main Configuration File
 * PHP 8.3+ | MySQL 8+
 */

define('APP_ROOT', dirname(__DIR__));

// ── Load .env file ────────────────────────────────────────────────────────────
$envFile = APP_ROOT . '/.env';
if (file_exists($envFile)) {
    $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach ($lines as $line) {
        $line = trim($line);
        if ($line === '' || str_starts_with($line, '#')) continue;
        if (str_contains($line, '=')) {
            [$key, $val] = explode('=', $line, 2);
            $key = trim($key);
            $val = trim($val);
            if (!isset($_ENV[$key]) && !isset($_SERVER[$key])) {
                $_ENV[$key]    = $val;
                $_SERVER[$key] = $val;
                putenv("{$key}={$val}");
            }
        }
    }
}

function env(string $key, mixed $default = null): mixed {
    $val = $_ENV[$key] ?? $_SERVER[$key] ?? getenv($key);
    if ($val === false || $val === null || $val === '') return $default;
    return match(strtolower((string)$val)) {
        'true'  => true,
        'false' => false,
        'null'  => null,
        default => $val,
    };
}
// ─────────────────────────────────────────────────────────────────────────────

define('APP_NAME',    'Zabibu Residence');
define('APP_VERSION', '1.0.0');
define('APP_URL',     rtrim(env('APP_URL', 'http://zabiburesidence.co.tz/'), '/'));
define('APP_DEBUG',   env('APP_DEBUG', false));
define('APP_ENV',     env('APP_ENV', 'production'));

define('VIEWS_PATH',   APP_ROOT . '/app/Views');
define('UPLOADS_PATH', APP_ROOT . '/public/uploads');
define('STORAGE_PATH', APP_ROOT . '/storage');

// Database
define('DB_HOST',    env('DB_HOST', 'localhost'));
define('DB_PORT',    env('DB_PORT', '3306'));
define('DB_NAME',    env('DB_NAME', 'zabiburesidence_db'));
define('DB_USER',    env('DB_USER', 'zabiburesidence_admin'));
define('DB_PASS',    env('DB_PASS', 'Tanzania12$'));
define('DB_CHARSET', 'utf8mb4');

// Application Settings
define('APP_TIMEZONE',        'Africa/Dar_es_Salaam');
define('APP_LOCALE',          'sw_TZ');
define('APP_CURRENCY',        'TZS');
define('APP_CURRENCY_SYMBOL', 'TZS');
define('APP_COUNTRY',         'Tanzania');

// Session
define('SESSION_LIFETIME', 7200);
define('SESSION_NAME',     'ZABIBU_SESSION');

// Security
define('CSRF_TOKEN_NAME',    '_csrf_token');
define('PASSWORD_MIN_LENGTH', 8);
define('MAX_LOGIN_ATTEMPTS',  5);
define('LOCKOUT_DURATION',    900);

// Email (SMTP)
define('MAIL_HOST',       env('MAIL_HOST',       'smtp.gmail.com'));
define('MAIL_PORT',       (int) env('MAIL_PORT',  587));
define('MAIL_USERNAME',   env('MAIL_USERNAME',   ''));
define('MAIL_PASSWORD',   env('MAIL_PASSWORD',   ''));
define('MAIL_FROM_NAME',  env('MAIL_FROM_NAME',  'Zabibu Residence'));
define('MAIL_ENCRYPTION', env('MAIL_ENCRYPTION', 'tls'));

// SMS Gateway
define('SMS_GATEWAY',    'beem');
define('SMS_API_KEY',    env('SMS_API_KEY',    ''));
define('SMS_API_SECRET', env('SMS_API_SECRET', ''));
define('SMS_SENDER_ID',  'ZABIBU');

// Payment Gateways
define('SELCOM_API_KEY',        env('SELCOM_API_KEY',        ''));
define('SELCOM_API_SECRET',     env('SELCOM_API_SECRET',     ''));
define('AZAMPAY_APP_NAME',      env('AZAMPAY_APP_NAME',      ''));
define('AZAMPAY_CLIENT_ID',     env('AZAMPAY_CLIENT_ID',     ''));
define('AZAMPAY_CLIENT_SECRET', env('AZAMPAY_CLIENT_SECRET', ''));

// File Uploads
define('MAX_FILE_SIZE',        10485760);
define('ALLOWED_IMAGE_TYPES',  ['image/jpeg', 'image/png', 'image/gif', 'image/webp']);
define('ALLOWED_DOC_TYPES',    ['application/pdf', 'image/jpeg', 'image/png']);

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

// Tax
define('VAT_RATE', 0.18); // 18% Tanzania VAT

date_default_timezone_set(APP_TIMEZONE);
