<?php

use CodeIgniter\Boot;
use Config\Paths;

/*
 *---------------------------------------------------------------
 * CHECK PHP VERSION
 *---------------------------------------------------------------
 */

$minPhpVersion = '8.1';
if (version_compare(PHP_VERSION, $minPhpVersion, '<')) {
  $message = sprintf(
    'Your PHP version must be %s or higher to run CodeIgniter. Current version: %s',
    $minPhpVersion,
    PHP_VERSION
  );

  header('HTTP/1.1 503 Service Unavailable.', true, 503);
  echo $message;
  exit(1);
}

/*
 *---------------------------------------------------------------
 * SET THE CURRENT DIRECTORY
 *---------------------------------------------------------------
 */

define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR);

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

/*
 *---------------------------------------------------------------
 * MAINTENANCE MODE (manual flag)
 *---------------------------------------------------------------
 */

$maintenanceFlag = '/home/pengprov/tkd2026/writable/maintenance.flag';

if (PHP_SAPI !== 'cli' && is_file($maintenanceFlag)) {
  http_response_code(503);
  header('Retry-After: 1800');
  header('Content-Type: text/html; charset=UTF-8');

  echo <<<'HTML'
<!doctype html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <meta name="robots" content="noindex,nofollow">
  <title>Maintenance</title>
  <style>
    body{margin:0;font-family:Arial,sans-serif;background:#f4f6f9;color:#1f2937}
    .wrap{min-height:100vh;display:flex;align-items:center;justify-content:center;padding:24px}
    .card{max-width:640px;width:100%;background:#fff;border-radius:12px;padding:28px;box-shadow:0 10px 30px rgba(0,0,0,.08);text-align:center}
    h1{margin:0 0 12px;font-size:28px}
    p{margin:0 0 8px;line-height:1.6}
    .muted{color:#6b7280;font-size:14px}
  </style>
</head>
<body>
  <div class="wrap">
    <div class="card">
      <h1>Website Sedang Dalam Perbaikan</h1>
      <p>Kami sedang melakukan maintenance untuk peningkatan sistem.</p>
      <p>Silakan coba kembali beberapa saat lagi.</p>
      <p class="muted">Terima kasih atas pengertiannya.</p>
    </div>
  </div>
</body>
</html>
HTML;

  exit(0);
}

/*
 *---------------------------------------------------------------
 * BOOTSTRAP THE APPLICATION
 *---------------------------------------------------------------
 */

$pathsPath = realpath(FCPATH . '../app/Config/Paths.php') ?: FCPATH . '../app/Config/Paths.php';

// MENGHARUSKAN file Paths.php dimuat sebelum memanggil class Config\Paths
require $pathsPath;

$paths = new Paths();

require $paths->systemDirectory . '/Boot.php';

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