<?php

declare(strict_types=1);

/**
 * Front controller — document root must point to /public.
 */

$root = dirname(__DIR__);
require $root . '/app/bootstrap.php';

use SeoScope\Http\WebDispatcher;
use SeoScope\i18n\TranslatorBootstrap;

$installLock = $root . '/storage/app/install.lock';
if (!is_readable($installLock)) {
    TranslatorBootstrap::registerDefault($root);
    header('Content-Type: text/html; charset=utf-8');
    $justInstalled = isset($_GET['installed']) && (string) $_GET['installed'] === '1';
    $title = __('install.required.title');
    $appName = __('app.name');
    $version = SEOSCOPE_VERSION;
    ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($title . ' — ' . $appName, ENT_QUOTES, 'UTF-8') ?></title>
    <link rel="stylesheet" href="/assets/fonts/inter/inter.css">
    <link rel="stylesheet" href="/assets/vendor/bootstrap/bootstrap.min.css">
    <link rel="stylesheet" href="/assets/vendor/fontawesome/css/all.min.css">
    <link rel="stylesheet" href="/assets/admin-template/css/admin-template.css">
</head>
<body class="login-page">
    <div class="login-shell">
        <div class="login-wrapper" style="max-width: 760px;">
            <div class="row g-0">
                <div class="col-12">
                    <div class="login-right" style="min-height: auto;">
                        <div class="login-card">
                            <div class="brand mb-4">
                                <span class="brand-icon"><i class="fa-solid fa-wave-square"></i></span>
                                <span><?= htmlspecialchars($appName, ENT_QUOTES, 'UTF-8') ?></span>
                            </div>
                            <h2 class="fw-800"><?= htmlspecialchars($title, ENT_QUOTES, 'UTF-8') ?></h2>
                            <p class="subtitle"><?= htmlspecialchars(__('install.required.body'), ENT_QUOTES, 'UTF-8') ?></p>

                            <?php if ($justInstalled): ?>
                                <div class="alert alert-success mb-3">
                                    <strong><?= htmlspecialchars(__('postinstall.just_installed'), ENT_QUOTES, 'UTF-8') ?></strong>
                                </div>
                            <?php endif; ?>

                            <a class="btn-login d-inline-flex align-items-center justify-content-center text-decoration-none" href="/install/index.php">
                                <?= htmlspecialchars(__('install.required.link'), ENT_QUOTES, 'UTF-8') ?>
                            </a>

                            <div class="mini-stats">
                                <div class="mini-stat">
                                    <div class="value"><?= htmlspecialchars($version, ENT_QUOTES, 'UTF-8') ?></div>
                                    <div class="label">Version</div>
                                </div>
                                <div class="mini-stat">
                                    <div class="value">Step</div>
                                    <div class="label">Installer</div>
                                </div>
                            </div>

                            <p class="helper-text mt-4 mb-0"><a href="/"><?= htmlspecialchars($appName, ENT_QUOTES, 'UTF-8') ?></a></p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="/assets/vendor/bootstrap/bootstrap.bundle.min.js"></script>
</body>
</html>
    <?php
    exit;
}

$configPath = $root . '/config/local.php';
if (!is_readable($configPath)) {
    // Use explicit file path to avoid rewrite loops on hosts
    // where the /install/ directory is not served directly.
    header('Location: /install/index.php', true, 302);
    exit;
}

/** @var array<string, mixed> $local */
$local = require $configPath;
/** @var array<string, mixed> $defaults */
$defaults = require $root . '/config/config.example.php';
/** @var array<string, mixed> $config */
$config = array_replace_recursive($defaults, $local);
$GLOBALS['seoscope_config'] = $config;
require_once $root . '/app/Views/http_helpers.php';

$uriPath = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
if (!is_string($uriPath) || $uriPath === '') {
    $uriPath = '/';
}
$basePath = seoscope_base_path();
if ($basePath !== '' && str_starts_with($uriPath, $basePath)) {
    $uriPath = substr($uriPath, strlen($basePath)) ?: '/';
}

\SeoScope\Auth\SessionAuth::start();
TranslatorBootstrap::registerForRequest($root, $config);

header('Content-Type: text/html; charset=utf-8');
header('X-Frame-Options: DENY');
header('X-Content-Type-Options: nosniff');
header('Referrer-Policy: same-origin');
header('Permissions-Policy: geolocation=(), microphone=(), camera=()');

$dispatcher = new WebDispatcher($root, $config, SEOSCOPE_TABLE_PREFIX);
$dispatcher->dispatch($_SERVER['REQUEST_METHOD'] ?? 'GET', $uriPath);
