<?php
// ============================================================
// Flexi Access Bank – Configuration
// Edit DB credentials before deploying
// ============================================================

define('DB_HOST', 'localhost');
define('DB_USER', 'flexiaccess_flexi2');
define('DB_PASS', 'Casper2017$$$');
define('DB_NAME', 'flexiaccess_flexi2');
define('DB_PORT', 3306);

define('SITE_URL', 'https://flexiaccess.site/flexiaccess');  // No trailing slash
define('SITE_NAME', 'Flexi Access Bank');
define('SITE_TAGLINE', 'Banking Without Boundaries');
define('SITE_EMAIL', 'noreply@flexiaccess.site');

define('SESSION_LIFETIME', 3600); // 1 hour
define('MAX_LOGIN_ATTEMPTS', 5);
define('LOCKOUT_TIME', 900); // 15 minutes
define('OTP_EXPIRY', 600); // 10 minutes

define('UPLOAD_PATH', __DIR__ . '/../uploads/');
define('AVATAR_PATH', __DIR__ . '/../uploads/avatars/');
define('KYC_PATH', __DIR__ . '/../uploads/kyc/');

ini_set('session.cookie_httponly', 1);
ini_set('session.use_only_cookies', 1);
ini_set('session.cookie_secure', isset($_SERVER['HTTPS']) ? 1 : 0);

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Database connection (PDO)
try {
    $dsn = "mysql:host=" . DB_HOST . ";port=" . DB_PORT . ";dbname=" . DB_NAME . ";charset=utf8mb4";
    $pdo = new PDO($dsn, DB_USER, DB_PASS, [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ]);
} catch (PDOException $e) {
    die('<div style="font-family:Arial;padding:40px;text-align:center;"><h2 style="color:#c0392b;">Database Connection Error</h2><p>Could not connect to the database. Please check your configuration.</p></div>');
}

// Load site settings from DB
function getSetting(PDO $pdo, string $key, string $default = ''): string {
    static $cache = [];
    if (!isset($cache[$key])) {
        $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = ? LIMIT 1");
        $stmt->execute([$key]);
        $row = $stmt->fetch();
        $cache[$key] = $row ? (string)$row['setting_value'] : $default;
    }
    return $cache[$key];
}
