<?php
declare(strict_types=1);

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

date_default_timezone_set('Asia/Baghdad');


$nayAdminDebug = strtolower((string)(getenv('NAY_ADMIN_DEBUG') ?: '0'));
$nayAdminDebugEnabled = in_array($nayAdminDebug, ['1', 'true', 'yes', 'on'], true);
ini_set('display_errors', $nayAdminDebugEnabled ? '1' : '0');
ini_set('display_startup_errors', $nayAdminDebugEnabled ? '1' : '0');
error_reporting(E_ALL);

$adminLocalConfig = __DIR__ . '/config.local.php';
if (is_file($adminLocalConfig)) {
    require_once $adminLocalConfig;
}

$host = isset($host) ? (string)$host : ((string)(getenv('NAY_DB_HOST') ?: 'localhost'));
$dbname = isset($dbname) ? (string)$dbname : ((string)(getenv('NAY_DB_NAME') ?: 'naysusam_v2'));
$username = isset($username) ? (string)$username : ((string)(getenv('NAY_DB_USER') ?: 'naysusam_v2'));
$password = isset($password) ? (string)$password : ((string)(getenv('NAY_DB_PASS') ?: 'Osamanay123@'));

$conn = new mysqli($host, $username, $password, $dbname);

if ($conn->connect_error) {
    die('Database Connection Failed: ' . $conn->connect_error);
}

$conn->set_charset('utf8mb4');

if (!function_exists('e')) {
    function e($value): string
    {
        return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
    }
}

if (!function_exists('base_url')) {
    function base_url(string $path = ''): string
    {
        return '/app/' . ltrim($path, '/');
    }
}

if (!function_exists('admin_url')) {
    function admin_url(string $path = ''): string
    {
        return '/app/admin/' . ltrim($path, '/');
    }
}

if (!function_exists('redirect_to')) {
    function redirect_to(string $url): void
    {
        header('Location: ' . $url);
        exit;
    }
}
