<?php

use CodeIgniter\Boot;

/*
 * CodeIgniter 4 Front Controller
 * Pazar Seasonings V18 - proper CI4 bootstrap.
 */
define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR);

/*
 * Compatibility fallback untuk XAMPP/PHP yang belum mengaktifkan extension mbstring.
 * Tetap disarankan mengaktifkan mbstring, tapi fallback ini mencegah fatal error awal.
 */
if (! function_exists('mb_strpos')) {
    function mb_strpos($haystack, $needle, $offset = 0, $encoding = null) { return strpos((string) $haystack, (string) $needle, (int) $offset); }
}
if (! function_exists('mb_strlen')) {
    function mb_strlen($string, $encoding = null) { return strlen((string) $string); }
}
if (! function_exists('mb_strtolower')) {
    function mb_strtolower($string, $encoding = null) { return strtolower((string) $string); }
}
if (! function_exists('mb_strtoupper')) {
    function mb_strtoupper($string, $encoding = null) { return strtoupper((string) $string); }
}
if (! function_exists('mb_substr')) {
    function mb_substr($string, $start, $length = null, $encoding = null) { return $length === null ? substr((string) $string, (int) $start) : substr((string) $string, (int) $start, (int) $length); }
}
if (! function_exists('mb_detect_encoding')) {
    function mb_detect_encoding($string, $encodings = null, $strict = false) { return 'UTF-8'; }
}
if (! function_exists('mb_convert_encoding')) {
    function mb_convert_encoding($string, $to_encoding, $from_encoding = null) { return (string) $string; }
}

if (! class_exists('Locale')) {
    class Locale {
        private static string $default = 'en';
        public static function setDefault($locale): bool { self::$default = (string) $locale; return true; }
        public static function getDefault(): string { return self::$default; }
        public static function acceptFromHttp($header): string { return self::$default; }
    }
}


if (getcwd() . DIRECTORY_SEPARATOR !== FCPATH) {
    chdir(FCPATH);
}

$pathsPath = FCPATH . '../app/Config/Paths.php';
if (! is_file($pathsPath)) {
    header('HTTP/1.1 503 Service Unavailable.', true, 503);
    echo 'CodeIgniter Paths.php tidak ditemukan.';
    exit(1);
}

require $pathsPath;
$paths = new Config\Paths();

$systemBoot = $paths->systemDirectory . '/Boot.php';
if (! is_file($systemBoot)) {
    header('HTTP/1.1 503 Service Unavailable.', true, 503);
    echo "CodeIgniter 4 core belum tersedia. Jalankan <b>composer install</b> di root project, lalu refresh halaman.";
    exit(1);
}

require $systemBoot;
exit(Boot::bootWeb($paths));
