<?php
/**
 * Flexia — pantalla de carga durante formateo / reinstalación.
 * Invocada desde el escritorio tras confirmar «Formatear y abrir instalador».
 */
declare(strict_types=1);

if (!defined('GRID_ROOT')) {
    define('GRID_ROOT', dirname(__DIR__));
}
if (!defined('ANTARA_ROOT')) {
    define('ANTARA_ROOT', GRID_ROOT);
}

@chdir(GRID_ROOT);

require_once GRID_ROOT . '/includes/install_public.php';
require_once GRID_ROOT . '/includes/vendor_assets.php';

if (!antara_install_is_locked()) {
    if (!headers_sent()) {
        header('Location: ' . antara_install_base_url(), true, 302);
        header('Cache-Control: no-store, no-cache, must-revalidate');
    }
    exit;
}

$keepExplorer = isset($_GET['keep']) && (string) $_GET['keep'] === '1';
$langRaw = strtolower(trim((string) ($_GET['lang'] ?? 'es')));
$lang = ($langRaw === 'en') ? 'en' : 'es';

$publicBase = rtrim(antara_public_app_base(), '/');
$apiUrl = $publicBase . '/index.php?action=prepareInstallWizard';
$appUrl = $publicBase . '/';
$installFallback = antara_install_base_url() . '?fresh=1';

$cssRel = 'assets/css/format-loading.css';
$jsRel = 'assets/js/format-loading.js';
$cssUrl = antara_vendor_asset_url($cssRel, $publicBase);
$jsUrl = antara_vendor_asset_url($jsRel, $publicBase);

$esc = static function (string $s): string {
    return htmlspecialchars($s, ENT_QUOTES, 'UTF-8');
};

$pageTitle = $lang === 'en' ? 'Formatting Flexia…' : 'Formateando Flexia…';
?>
<!DOCTYPE html>
<html lang="<?php echo $esc($lang); ?>">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="robots" content="noindex, nofollow">
    <title><?php echo $esc($pageTitle); ?></title>
    <link rel="stylesheet" href="<?php echo $esc($cssUrl); ?>">
</head>
<body class="flexia-format-load">
    <svg aria-hidden="true" width="0" height="0" style="position:absolute">
        <defs>
            <linearGradient id="fmt-ring-gradient" x1="0%" y1="0%" x2="100%" y2="100%">
                <stop offset="0%" stop-color="#38bdf8"/>
                <stop offset="55%" stop-color="#a78bfa"/>
                <stop offset="100%" stop-color="#34d399"/>
            </linearGradient>
            <filter id="fmt-displace" x="-20%" y="-20%" width="140%" height="140%">
                <feTurbulence type="fractalNoise" baseFrequency="0.012" numOctaves="3" seed="42" result="noise">
                    <animate attributeName="baseFrequency" dur="12s" values="0.01;0.018;0.012;0.01" repeatCount="indefinite"/>
                </feTurbulence>
                <feDisplacementMap in="SourceGraphic" in2="noise" scale="28" xChannelSelector="R" yChannelSelector="G">
                    <animate attributeName="scale" dur="8s" values="18;42;24;18" repeatCount="indefinite"/>
                </feDisplacementMap>
            </filter>
        </defs>
    </svg>

    <div class="flexia-format-load__bg" aria-hidden="true"></div>
    <div class="flexia-format-load__bg-warp" aria-hidden="true"></div>
    <div class="flexia-format-load__grain" aria-hidden="true"></div>

    <div class="flexia-format-load__viewport">
        <div class="flexia-format-load__crystal-scene" aria-hidden="true">
            <div class="flexia-format-load__crystal">
                <div class="flexia-format-load__crystal-face"></div>
                <div class="flexia-format-load__crystal-face"></div>
                <div class="flexia-format-load__crystal-face"></div>
                <div class="flexia-format-load__crystal-face"></div>
                <div class="flexia-format-load__crystal-face"></div>
                <div class="flexia-format-load__crystal-face"></div>
            </div>
        </div>

        <main class="flexia-format-load__panel" role="main" aria-live="polite" aria-busy="true">
            <div class="flexia-format-load__brand" id="fmtBrand">Flexia</div>
            <h1 class="flexia-format-load__title" id="fmtTitle"><?php echo $lang === 'en' ? 'Formatting instance' : 'Formateando instancia'; ?></h1>
            <p class="flexia-format-load__subtitle" id="fmtSubtitle"></p>

            <div class="flexia-format-load__ring-wrap">
                <svg class="flexia-format-load__ring" viewBox="0 0 100 100" aria-hidden="true">
                    <circle class="flexia-format-load__ring-track" cx="50" cy="50" r="45"/>
                    <circle class="flexia-format-load__ring-progress" id="fmtRingProgress" cx="50" cy="50" r="45"/>
                </svg>
                <div class="flexia-format-load__pct">
                    <span class="flexia-format-load__pct-value" id="fmtPctValue">0</span>
                    <span class="flexia-format-load__pct-label" id="fmtPctLabel">progreso</span>
                </div>
            </div>

            <p class="flexia-format-load__phase" id="fmtPhase"></p>

            <div class="flexia-format-load__bar-track" aria-hidden="true">
                <div class="flexia-format-load__bar-fill" id="fmtBarFill"></div>
            </div>

            <div class="flexia-format-load__meta">
                <span><span id="fmtElapsedLabel">Transcurrido</span>: <span id="fmtElapsed">0 s</span></span>
                <span><span id="fmtEtaLabel">Estimado</span>: <span id="fmtEta">—</span></span>
            </div>

            <div class="flexia-format-load__error" id="fmtError" role="alert"></div>
            <button type="button" class="flexia-format-load__back" id="fmtBackBtn"></button>
        </main>
    </div>

    <script>
    window.FLEXIA_FORMAT_LOAD = <?php echo json_encode([
        'lang' => $lang,
        'keepExplorerFiles' => $keepExplorer,
        'apiUrl' => $apiUrl,
        'appUrl' => $appUrl,
        'installFallback' => $installFallback,
    ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP); ?>;
    </script>
    <script src="<?php echo $esc($jsUrl); ?>" defer></script>
</body>
</html>
