<?php
$localeNames = ['pt' => 'PT', 'en' => 'EN', 'es' => 'ES'];
$primaryNavItems = [
    ['href' => site_url('/') . '#problem', 'label' => lang('Landing.navProblem')],
    ['href' => site_url('/') . '#solution', 'label' => lang('Landing.navSolution')],
];
$platformNavItems = [
    ['href' => site_url('/') . '#features', 'label' => lang('Landing.navFeatures')],
    ['href' => site_url('/') . '#benefits', 'label' => lang('Landing.navBenefits')],
    ['href' => site_url('/') . '#technology', 'label' => lang('Landing.navTechnology')],
];
$secondaryNavItems = [
    ['href' => site_url('/') . '#contact', 'label' => lang('Landing.navContact')],
];
$publicUi = [
    'pt' => [
        'platformMenu' => 'Plataforma',
        'auditMenu' => 'Auditoria',
        'companyCaption' => 'Projeto da Quantumbridge, Lda',
        'footerAttribution' => 'Projeto desenvolvido pela Quantumbridge, Lda.',
        'footerLogosAlt' => 'Barra institucional de logotipos Quantumbridge',
        'mobileMenuTitle' => 'Navegacao',
        'backToSite' => 'Voltar ao site',
        'auditPortal' => 'Portal de Auditoria Quantum',
        'auditLead' => 'Pesquisa uma intervencao tecnica por codigo, referencia externa, transaction hash ou anchored hash e valida o respetivo trilho de imutabilidade.',
        'searchButton' => 'Pesquisar',
        'landingLink' => 'Landing',
        'resultLabel' => 'Resultado',
        'notFound' => 'Nenhum registo encontrado para a pesquisa indicada.',
        'organization' => 'Organizacao',
        'site' => 'Parque eolico',
        'turbine' => 'Turbina',
        'status' => 'Estado',
        'type' => 'Tipo',
        'reference' => 'Referencia externa',
        'network' => 'Rede blockchain',
        'blockchainStatus' => 'Estado blockchain',
        'transactionHash' => 'Transaction hash',
        'anchoredHash' => 'Anchored hash',
    ],
    'en' => [
        'platformMenu' => 'Platform',
        'auditMenu' => 'Audit',
        'companyCaption' => 'A Quantumbridge, Lda project',
        'footerAttribution' => 'Project developed by Quantumbridge, Lda.',
        'footerLogosAlt' => 'Quantumbridge institutional logo bar',
        'mobileMenuTitle' => 'Navigation',
        'backToSite' => 'Back to site',
        'auditPortal' => 'Quantum Audit Portal',
        'auditLead' => 'Search a technical intervention by code, external reference, transaction hash or anchored hash and validate its immutability trail.',
        'searchButton' => 'Search',
        'landingLink' => 'Landing',
        'resultLabel' => 'Result',
        'notFound' => 'No record found for the provided search.',
        'organization' => 'Organization',
        'site' => 'Wind farm',
        'turbine' => 'Turbine',
        'status' => 'Status',
        'type' => 'Type',
        'reference' => 'External reference',
        'network' => 'Blockchain network',
        'blockchainStatus' => 'Blockchain status',
        'transactionHash' => 'Transaction hash',
        'anchoredHash' => 'Anchored hash',
    ],
    'es' => [
        'platformMenu' => 'Plataforma',
        'auditMenu' => 'Auditoria',
        'companyCaption' => 'Un proyecto de Quantumbridge, Lda',
        'footerAttribution' => 'Proyecto desarrollado por Quantumbridge, Lda.',
        'footerLogosAlt' => 'Barra institucional de logotipos Quantumbridge',
        'mobileMenuTitle' => 'Navegacion',
        'backToSite' => 'Volver al sitio',
        'auditPortal' => 'Portal de Auditoria Quantum',
        'auditLead' => 'Busca una intervencion tecnica por codigo, referencia externa, transaction hash o anchored hash y valida su rastro de inmutabilidad.',
        'searchButton' => 'Buscar',
        'landingLink' => 'Landing',
        'resultLabel' => 'Resultado',
        'notFound' => 'No se encontro ningun registro para la busqueda indicada.',
        'organization' => 'Organizacion',
        'site' => 'Parque eolico',
        'turbine' => 'Turbina',
        'status' => 'Estado',
        'type' => 'Tipo',
        'reference' => 'Referencia externa',
        'network' => 'Red blockchain',
        'blockchainStatus' => 'Estado blockchain',
        'transactionHash' => 'Transaction hash',
        'anchoredHash' => 'Anchored hash',
    ],
];
$ui = $publicUi[$currentLocale ?? 'pt'] ?? $publicUi['pt'];
?>
<!DOCTYPE html>
<html lang="<?= esc($currentLocale ?? 'pt') ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Quantum Audit</title>
    <link rel="icon" type="image/png" href="<?= base_url('assets/icon.png') ?>">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;500;700&family=IBM+Plex+Sans:wght@400;500;600;700&family=Sora:wght@400;500;600;700&display=swap" rel="stylesheet">
    <script>
        (() => {
            try {
                const theme = localStorage.getItem('quantum-public-theme') || 'dark';
                document.documentElement.setAttribute('data-theme', theme);
            } catch (error) {
                document.documentElement.setAttribute('data-theme', 'dark');
            }
        })();
    </script>
    <link rel="stylesheet" href="<?= base_url('assets/quantum-public.css') ?>">
</head>
<body>
    <header class="site-header">
        <div class="container">
            <div class="header-row">
                <a href="<?= site_url('/') ?>" class="brand" aria-label="Quantum">
                    <img class="brand-mark" src="<?= base_url('assets/quantum-wordmark.svg') ?>" alt="Quantum logo">
                </a>

                <div class="nav-wrap">
                    <nav class="nav-links" aria-label="Primary">
                        <?php foreach ($primaryNavItems as $item): ?>
                            <a href="<?= esc($item['href']) ?>" class="nav-link"><?= esc($item['label']) ?></a>
                        <?php endforeach; ?>
                        <div class="nav-dropdown">
                            <button type="button" class="nav-dropdown__toggle" aria-haspopup="true">
                                <span><?= esc($ui['platformMenu']) ?></span>
                                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m6 9 6 6 6-6"></path></svg>
                            </button>
                            <div class="nav-submenu">
                                <?php foreach ($platformNavItems as $item): ?>
                                    <a href="<?= esc($item['href']) ?>" class="nav-link nav-submenu__link"><?= esc($item['label']) ?></a>
                                <?php endforeach; ?>
                            </div>
                        </div>
                        <?php foreach ($secondaryNavItems as $item): ?>
                            <a href="<?= esc($item['href']) ?>" class="nav-link"><?= esc($item['label']) ?></a>
                        <?php endforeach; ?>
                    </nav>
                </div>

                <div class="header-actions">
                    <div class="lang-shell" aria-label="<?= esc(lang('Landing.languageLabel')) ?>">
                        <?php foreach ($supportedLocales as $locale): ?>
                            <a href="<?= site_url('lang/' . $locale) ?>" class="lang-option <?= ($currentLocale ?? 'pt') === $locale ? 'active' : '' ?>">
                                <?= esc($localeNames[$locale] ?? strtoupper($locale)) ?>
                            </a>
                        <?php endforeach; ?>
                    </div>
                    <button class="button button-secondary theme-toggle" type="button" data-theme-toggle aria-label="Toggle theme" title="Toggle theme">
                        <span class="theme-icon-light" aria-hidden="true">
                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8Z"></path></svg>
                        </span>
                        <span class="theme-icon-dark" aria-hidden="true">
                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="4"></circle><path d="M12 2v2.2M12 19.8V22M4.93 4.93l1.55 1.55M17.52 17.52l1.55 1.55M2 12h2.2M19.8 12H22M4.93 19.07l1.55-1.55M17.52 6.48l1.55-1.55"></path></svg>
                        </span>
                        <span class="sr-only">Alternar tema</span>
                    </button>
                    <a href="<?= site_url('/') ?>" class="button button-secondary"><?= esc($ui['landingLink']) ?></a>
                    <a href="<?= site_url('audit') ?>" class="button button-primary"><?= esc($ui['auditMenu']) ?></a>
                </div>

                <button class="menu-toggle" id="menuToggle" type="button" aria-expanded="false" aria-controls="mobileMenu" aria-label="<?= esc(lang('Landing.menuLabel')) ?>">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round"><path d="M4 7h16"></path><path d="M4 12h16"></path><path d="M4 17h16"></path></svg>
                </button>
            </div>

            <div class="mobile-menu" id="mobileMenu">
                <div class="mobile-panel">
                    <div class="mobile-group-title"><?= esc($ui['mobileMenuTitle']) ?></div>
                    <?php foreach (array_merge($primaryNavItems, $platformNavItems, $secondaryNavItems) as $item): ?>
                        <a href="<?= esc($item['href']) ?>" class="nav-link"><?= esc($item['label']) ?></a>
                    <?php endforeach; ?>
                    <a href="<?= site_url('/') ?>" class="button button-secondary"><?= esc($ui['landingLink']) ?></a>
                    <a href="<?= site_url('audit') ?>" class="button button-secondary"><?= esc($ui['auditMenu']) ?></a>
                    <button class="button button-secondary theme-toggle" type="button" data-theme-toggle aria-label="Toggle theme" title="Toggle theme">
                        <span class="theme-icon-light" aria-hidden="true">
                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8Z"></path></svg>
                        </span>
                        <span class="theme-icon-dark" aria-hidden="true">
                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="4"></circle><path d="M12 2v2.2M12 19.8V22M4.93 4.93l1.55 1.55M17.52 17.52l1.55 1.55M2 12h2.2M19.8 12H22M4.93 19.07l1.55-1.55M17.52 6.48l1.55-1.55"></path></svg>
                        </span>
                        <span class="sr-only">Alternar tema</span>
                    </button>
                </div>
            </div>
        </div>
    </header>

    <main class="audit-shell">
        <div class="container">
            <section class="audit-card">
                <div class="audit-toolbar">
                    <div>
                        <span class="eyebrow">Audit portal</span>
                        <h1 style="margin-top:1rem;"><?= esc($ui['auditPortal']) ?></h1>
                        <p style="margin-top:1rem;"><?= esc($ui['auditLead']) ?></p>
                    </div>
                    <div class="audit-actions">
                        <a href="<?= site_url('/') ?>" class="button button-secondary"><?= esc($ui['backToSite']) ?></a>
                    </div>
                </div>

                <form method="get" action="<?= site_url('audit') ?>" class="audit-form">
                    <input type="text" name="q" value="<?= esc($query) ?>" placeholder="MI-2026-0001 ou 0x...">
                    <button type="submit" class="button button-primary"><?= esc($ui['searchButton']) ?></button>
                </form>
            </section>

            <?php if ($query !== '' && ! $result): ?>
                <section class="audit-result" style="margin-top:1rem;">
                    <div class="label"><?= esc($ui['resultLabel']) ?></div>
                    <div class="value"><?= esc($ui['notFound']) ?></div>
                </section>
            <?php endif; ?>

            <?php if ($result): ?>
                <section class="audit-result" style="margin-top:1rem;">
                    <div class="label"><?= esc($ui['resultLabel']) ?></div>
                    <h2 style="margin-top:0.8rem;"><?= esc($result['intervention_code']) ?></h2>
                    <p style="margin-top:0.6rem;"><?= esc($result['title']) ?></p>

                    <div class="audit-grid">
                        <div><div class="label"><?= esc($ui['organization']) ?></div><div class="value"><?= esc($result['organization_name'] ?? '-') ?></div></div>
                        <div><div class="label"><?= esc($ui['site']) ?></div><div class="value"><?= esc($result['site_name'] ?? '-') ?></div></div>
                        <div><div class="label"><?= esc($ui['turbine']) ?></div><div class="value"><?= esc($result['turbine_code'] ?? '-') ?></div></div>
                        <div><div class="label"><?= esc($ui['status']) ?></div><div class="value"><?= esc($result['status']) ?></div></div>
                        <div><div class="label"><?= esc($ui['type']) ?></div><div class="value"><?= esc($result['intervention_type']) ?></div></div>
                        <div><div class="label"><?= esc($ui['reference']) ?></div><div class="value"><?= esc($result['external_reference'] ?: '-') ?></div></div>
                        <div><div class="label"><?= esc($ui['network']) ?></div><div class="value"><?= esc($result['network'] ?: '-') ?></div></div>
                        <div><div class="label"><?= esc($ui['blockchainStatus']) ?></div><div class="value"><?= esc($result['blockchain_status'] ?: '-') ?></div></div>
                        <div style="grid-column:1 / -1;"><div class="label"><?= esc($ui['transactionHash']) ?></div><div class="value"><?= esc($result['transaction_hash'] ?: '-') ?></div></div>
                        <div style="grid-column:1 / -1;"><div class="label"><?= esc($ui['anchoredHash']) ?></div><div class="value"><?= esc($result['anchored_hash'] ?: '-') ?></div></div>
                    </div>
                </section>
            <?php endif; ?>
        </div>
    </main>

    <footer class="footer">
        <div class="container">
            <div class="footer-row">
                <div>
                    <div><?= esc(lang('Landing.footer')) ?></div>
                    <div class="footer-caption"><?= esc($ui['footerAttribution']) ?></div>
                </div>
                <a href="<?= site_url('/') ?>" class="button button-ghost"><?= esc($ui['backToSite']) ?></a>
            </div>

            <div class="footer-logos">
                <img src="<?= base_url('assets/barra-logos.png') ?>" alt="<?= esc($ui['footerLogosAlt']) ?>">
            </div>
        </div>
    </footer>

    <script>
    (() => {
        const themeKey = 'quantum-public-theme';
        const themeButtons = document.querySelectorAll('[data-theme-toggle]');
        const applyTheme = (theme) => {
            document.documentElement.setAttribute('data-theme', theme);
            themeButtons.forEach((button) => {
                button.setAttribute('aria-pressed', theme === 'dark' ? 'true' : 'false');
            });
        };

        applyTheme(document.documentElement.getAttribute('data-theme') || 'dark');

        themeButtons.forEach((button) => {
            button.addEventListener('click', () => {
                const nextTheme = (document.documentElement.getAttribute('data-theme') || 'dark') === 'light' ? 'dark' : 'light';
                applyTheme(nextTheme);
                try {
                    localStorage.setItem(themeKey, nextTheme);
                } catch (error) {
                    // Ignore storage restrictions.
                }
            });
        });

        const menuToggle = document.getElementById('menuToggle');
        const mobileMenu = document.getElementById('mobileMenu');
        const mobileMenuLinks = mobileMenu ? mobileMenu.querySelectorAll('a') : [];

        const closeMenu = () => {
            if (!mobileMenu || !menuToggle) return;
            mobileMenu.classList.remove('open');
            menuToggle.setAttribute('aria-expanded', 'false');
        };

        const openMenu = () => {
            if (!mobileMenu || !menuToggle) return;
            mobileMenu.classList.add('open');
            menuToggle.setAttribute('aria-expanded', 'true');
        };

        if (menuToggle && mobileMenu) {
            menuToggle.addEventListener('click', () => {
                const isOpen = mobileMenu.classList.contains('open');
                if (isOpen) closeMenu();
                else openMenu();
            });

            mobileMenuLinks.forEach((link) => link.addEventListener('click', closeMenu));

            window.addEventListener('resize', () => {
                if (window.innerWidth > 840) closeMenu();
            });
        }

        document.addEventListener('keydown', (event) => {
            if (event.key === 'Escape') {
                closeMenu();
            }
        });
    })();
    </script>
</body>
</html>
