<?php

use Illuminate\Http\Request;

// =======================================================================================
// Pre-Check for System Requirements
// =======================================================================================
require 'precheck/system.php';
require 'precheck/hidden-files.php';
// Pre-v19 → v19 file-overlay upgrades: invalidate bootstrap/cache and
// storage/framework cache state generated against the pre-v19 vendor
// tree before Laravel's autoloader tries to use it. No-op once `.env`
// no longer carries a stale `APP_VERSION` (i.e., after the bridge runs).
require 'precheck/legacy-upgrade.php';

// =======================================================================================
// Fix SERVER variables so Symfony's base URL detection works correctly in two
// non-standard deployment architectures that both use the root .htaccess
// transparent rewrite  (RewriteRule ^(.*)$ public/$1 [L]).
//
// ─── Case 1: subdirectory install (e.g., https://site.com/website/) ───
//
//   Apache internally dispatches /website/public/index.php for a request to
//   /website/install.  PHP receives:
//     REQUEST_URI  = /website/install           (original: correct)
//     SCRIPT_NAME  = /website/public/index.php  (internal: has extra /public)
//
//   Symfony tries to match SCRIPT_NAME against REQUEST_URI to derive the base
//   URL.  "/website/public" is not a prefix of "/website/install", so it falls
//   back to an empty base.  The router then sees "website/install" as the full
//   path, the installer route never matches, and the front-end catch-all fires.
//
//   Fix: strip "/public" from SCRIPT_NAME so Symfony sees "/website/index.php"
//   and correctly detects "/website" as the base URL.
//
// ─── Case 2: root install, domain points to app root (not to public/) ───
//
//   On some hosts Apache updates REQUEST_URI to the rewritten path, so PHP
//   receives the "/public/" prefix that was added by the rewrite rule:
//     REQUEST_URI  = /public/admin/   (rewritten: wrong)
//     SCRIPT_NAME  = /public/index.php
//
//   Symfony finds "/public/" as a prefix of "/public/admin/" and returns
//   "/public" as the base URL.  Every generated route URL then contains a
//   spurious "/public/" segment (e.g., /public/admin/login instead of
//   /admin/login).
//
//   Fix: strip the "/public" prefix from REQUEST_URI so Symfony returns an
//   empty base URL, which is correct for a root installation.
// =======================================================================================
(static function (): void {
	$scriptName = $_SERVER['SCRIPT_NAME'] ?? '';
	$requestUri = $_SERVER['REQUEST_URI'] ?? '';
	
	if (!$scriptName || !str_ends_with(dirname($scriptName), '/public')) {
		return;
	}
	
	$publicDir = dirname($scriptName);      // e.g., /public  or  /website/public
	$appDir = dirname($publicDir);          // e.g., /        or  /website
	
	if ($appDir !== '/' && $appDir !== '\\' && $appDir !== '') {
		// ─── Case 1: subdirectory install ───
		// Strip "/public" from SCRIPT_NAME so Symfony detects the subdirectory
		// prefix (e.g., /website) as the base URL.
		$_SERVER['SCRIPT_NAME'] = $appDir . '/index.php';
		$_SERVER['PHP_SELF'] = $appDir . '/index.php';
		
		return;
	}
	
	// ─── Case 2: root install, domain → app root ───
	// Strip the spurious "/public" prefix that Apache's rewrite added to
	// REQUEST_URI so Symfony returns an empty base URL (correct for root).
	if ($requestUri === '/public' || str_starts_with($requestUri, '/public/')) {
		$_SERVER['REQUEST_URI'] = '/' . ltrim(substr($requestUri, strlen('/public')), '/');
	}
})();

// =======================================================================================
// Laravel Bootstrap
// =======================================================================================
define('LARAVEL_START', microtime(true));

// Determine if the application is in maintenance mode...
if (file_exists($maintenance = __DIR__ . '/../storage/framework/maintenance.php')) {
	require $maintenance;
}

// Register the Composer autoloader...
require __DIR__ . '/../vendor/autoload.php';

// Bootstrap Laravel and handle the request...
(require_once __DIR__ . '/../bootstrap/app.php')->handleRequest(Request::capture());
