<?php

declare(strict_types=1);

require_once __DIR__ . '/library.php';

function library_active_nav(string $current, string $expected): string
{
    return $current === $expected ? 'active' : '';
}

function library_render_header(string $pageTitle, string $pageDescription, string $activeNav = 'catalog'): void
{
    $lang = library_get_language();
    $isRtl = $lang === 'ar';
    $profile = library_get_profile();
    $project = library_project_meta();
    $metaDescription = $pageDescription !== '' ? $pageDescription : $project['description'];
    $projectImageUrl = $project['image'];
    $fullTitle = $pageTitle . ' · ' . $project['name'];
    $flashes = library_get_flashes();
    $targetLang = $isRtl ? 'en' : 'ar';
    $switchLabel = library_trans('switch_lang', $lang);
    $profileLabel = $isRtl ? 'صفحتي' : 'My Activity';
    $brandName = library_profile_name($lang);
    $brandTagline = library_profile_tagline($lang);
    $brandShortName = trim((string) ($profile['short_name'] ?? '')) ?: 'NL';
    $logoPath = trim((string) ($profile['logo_path'] ?? ''));
    $faviconPath = trim((string) ($profile['favicon_path'] ?? ''));

    $params = $_GET;
    $params['lang'] = $targetLang;
    $switchUrl = '?' . http_build_query($params);
    ?>
<!doctype html>
<html lang="<?= h($lang) ?>" dir="<?= $isRtl ? 'rtl' : 'ltr' ?>">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= h($fullTitle) ?></title>
    <meta name="description" content="<?= h($metaDescription) ?>">
    <meta property="og:title" content="<?= h($fullTitle) ?>">
    <meta property="og:description" content="<?= h($metaDescription) ?>">
    <meta property="twitter:title" content="<?= h($fullTitle) ?>">
    <meta property="twitter:description" content="<?= h($metaDescription) ?>">
<?php if ($projectImageUrl): ?>
    <meta property="og:image" content="<?= h($projectImageUrl) ?>">
    <meta property="twitter:image" content="<?= h($projectImageUrl) ?>">
<?php endif; ?>
<?php if ($faviconPath !== ''): ?>
    <link rel="icon" href="/<?= h($faviconPath) ?>?v=<?= time() ?>" sizes="any">
    <link rel="shortcut icon" href="/<?= h($faviconPath) ?>?v=<?= time() ?>">
<?php endif; ?>
    <?php if ($isRtl): ?>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.rtl.min.css" integrity="sha384-dpuaG1suU0eT09tx5plTaGMLBsfDLzUCCUXOY2j/LSvXYuG6Bqs43ALlhIqAJVRb" crossorigin="anonymous">
    <?php else: ?>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    <?php endif; ?>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <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=Cairo:wght@400;500;600;700;800&family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="/assets/css/custom.css?v=<?= time() ?>">
</head>
<body>
<div class="app-shell cinematic-library-shell">
    <div class="library-backdrop" aria-hidden="true">
        <span class="backdrop-orb orb-cyan"></span>
        <span class="backdrop-orb orb-amber"></span>
        <span class="backdrop-grid"></span>
        <span class="backdrop-books"></span>
        <span class="backdrop-scripture"></span>
        <span class="backdrop-column column-left"></span>
        <span class="backdrop-column column-right"></span>
    </div>
    <nav class="navbar navbar-expand-lg sticky-top library-topbar">
        <div class="container library-nav-shell">
            <a class="navbar-brand d-flex align-items-center gap-3" href="/index.php">
                <?php if ($logoPath !== ''): ?>
                    <img class="brand-logo" src="/<?= h($logoPath) ?>?v=<?= time() ?>" alt="<?= h($brandName) ?> logo">
                <?php else: ?>
                    <span class="brand-mark"><?= h($brandShortName) ?></span>
                <?php endif; ?>
                <span class="brand-lockup">
                    <span class="d-block brand-title"><?= h($brandName) ?></span>
                    <?php if ($brandTagline !== ''): ?>
                        <small class="text-secondary d-block"><?= h($brandTagline) ?></small>
                    <?php endif; ?>
                </span>
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#libraryNav" aria-controls="libraryNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="libraryNav">
                <ul class="navbar-nav ms-auto align-items-lg-center gap-lg-2">
                    <li class="nav-item"><a class="nav-link <?= library_active_nav($activeNav, 'catalog') ?>" href="/index.php"><?= library_trans('catalog') ?></a></li>
                    <li class="nav-item"><a class="nav-link <?= library_active_nav($activeNav, 'profile') ?>" href="/user.php"><?= h($profileLabel) ?></a></li>
                    <li class="nav-item"><a class="nav-link <?= library_active_nav($activeNav, 'admin') ?>" href="/admin.php"><?= library_trans('admin_studio') ?></a></li>
                    <li class="nav-item border-start border-secondary mx-2 d-none d-lg-block" style="opacity: 0.3; height: 24px;"></li>
                    <li class="nav-item">
                        <a class="btn btn-sm btn-outline-dark topbar-lang-switch" href="<?= h($switchUrl) ?>" lang="<?= h($targetLang) ?>" dir="<?= $targetLang === 'ar' ? 'rtl' : 'ltr' ?>">
                            <?= h($switchLabel) ?>
                        </a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <main class="pb-5 library-main">
        <div class="container py-4 py-lg-5 library-stage">
<?php if ($flashes): ?>
            <div class="toast-stack position-fixed top-0 end-0 p-3">
<?php foreach ($flashes as $flash): ?>
                <div class="toast border-0 shadow-sm" role="status" aria-live="polite" aria-atomic="true" data-bs-delay="4500">
                    <div class="toast-header text-bg-<?= h($flash['type'] === 'danger' ? 'danger' : ($flash['type'] === 'warning' ? 'warning' : 'dark')) ?> border-0">
                        <strong class="me-auto">Library update</strong>
                        <button type="button" class="btn-close btn-close-white ms-2 mb-1" data-bs-dismiss="toast" aria-label="Close"></button>
                    </div>
                    <div class="toast-body bg-white"><?= h($flash['message']) ?></div>
                </div>
<?php endforeach; ?>
            </div>
<?php endif; ?>

function library_render_footer(): void
{
    $lang = library_get_language();
    $isRtl = $lang === 'ar';
    $profile = library_get_profile();
    $brandName = library_profile_name($lang);
    $brandTagline = library_profile_tagline($lang);
    $brandDescription = library_profile_description($lang);
    $brandShortName = trim((string) ($profile['short_name'] ?? '')) ?: 'NL';
    $logoPath = trim((string) ($profile['logo_path'] ?? ''));
    $address = library_profile_address($lang);
    $openingHours = library_profile_opening_hours($lang);
    $copyright = library_localized_value(
        $profile['copyright_text_en'] ?? null,
        $profile['copyright_text_ar'] ?? null,
        $lang,
        ''
    );
    $socialLinks = [
        ['label' => 'Facebook', 'icon' => 'bi-facebook', 'url' => trim((string) ($profile['facebook_url'] ?? ''))],
        ['label' => 'Instagram', 'icon' => 'bi-instagram', 'url' => trim((string) ($profile['instagram_url'] ?? ''))],
        ['label' => 'X', 'icon' => 'bi-twitter-x', 'url' => trim((string) ($profile['x_url'] ?? ''))],
        ['label' => 'YouTube', 'icon' => 'bi-youtube', 'url' => trim((string) ($profile['youtube_url'] ?? ''))],
    ];
    ?>
        </div>
    </main>

    <footer class="library-footer">
        <div class="container">
            <div class="library-footer-panel py-4 px-4 px-lg-5">
                <div class="row g-4 align-items-start">
                    <div class="col-lg-5">
                        <div class="footer-brand d-flex gap-3 align-items-start">
                            <?php if ($logoPath !== ''): ?>
                                <img class="brand-logo footer-brand-logo" src="/<?= h($logoPath) ?>?v=<?= time() ?>" alt="<?= h($brandName) ?> logo">
                            <?php else: ?>
                                <span class="brand-mark"><?= h($brandShortName) ?></span>
                            <?php endif; ?>
                            <div>
                                <div class="fw-semibold footer-title mb-1"><?= h($brandName) ?></div>
                                <?php if ($brandTagline !== ''): ?>
                                    <div class="footer-copy mb-2"><?= h($brandTagline) ?></div>
                                <?php endif; ?>
                                <div class="footer-copy footer-description"><?= h($brandDescription) ?></div>
                                <?php if ($copyright !== ''): ?>
                                    <div class="footer-meta-note mt-3"><?= h($copyright) ?></div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-4">
                        <div class="footer-section-title mb-3"><?= $isRtl ? 'تفاصيل المكتبة' : 'Library details' ?></div>
                        <div class="footer-detail-list small">
                            <?php if (!empty($profile['contact_email'])): ?>
                                <div><i class="bi bi-envelope"></i> <a href="mailto:<?= h($profile['contact_email']) ?>"><?= h($profile['contact_email']) ?></a></div>
                            <?php endif; ?>
                            <?php if (!empty($profile['contact_phone'])): ?>
                                <div><i class="bi bi-telephone"></i> <span><?= h($profile['contact_phone']) ?></span></div>
                            <?php endif; ?>
                            <?php if (!empty($profile['whatsapp_number'])): ?>
                                <div><i class="bi bi-whatsapp"></i> <span><?= h($profile['whatsapp_number']) ?></span></div>
                            <?php endif; ?>
                            <?php if (!empty($profile['website_url'])): ?>
                                <div><i class="bi bi-globe2"></i> <a href="<?= h($profile['website_url']) ?>" target="_blank" rel="noopener noreferrer"><?= $isRtl ? 'الموقع الإلكتروني' : 'Website' ?></a></div>
                            <?php endif; ?>
                            <?php if ($address !== ''): ?>
                                <div><i class="bi bi-geo-alt"></i> <span dir="<?= library_text_dir($address, $lang) ?>"><?= h($address) ?></span></div>
                            <?php endif; ?>
                            <?php if ($openingHours !== ''): ?>
                                <div><i class="bi bi-clock"></i> <span dir="<?= library_text_dir($openingHours, $lang) ?>"><?= h($openingHours) ?></span></div>
                            <?php endif; ?>
                        </div>
                    </div>
                    <div class="col-lg-3">
                        <div class="footer-section-title mb-3"><?= $isRtl ? 'روابط سريعة' : 'Quick links' ?></div>
                        <div class="footer-links mb-3">
                            <div><a class="text-decoration-none" href="/user.php"><?= h($isRtl ? 'سجل نشاطي' : 'My activity log') ?></a></div>
                            <div><a class="text-decoration-none" href="/admin.php"><?= library_trans('open_admin') ?></a></div>
                            <div><a class="text-decoration-none" href="/index.php"><?= library_trans('browse_public') ?></a></div>
                        </div>
                        <div class="footer-socials">
                            <?php foreach ($socialLinks as $social): ?>
                                <?php if ($social['url'] === '') { continue; } ?>
                                <a href="<?= h($social['url']) ?>" target="_blank" rel="noopener noreferrer" aria-label="<?= h($social['label']) ?>">
                                    <i class="bi <?= h($social['icon']) ?>"></i>
                                </a>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </footer>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script src="/assets/js/main.js?v=<?= time() ?>"></script>
</body>
</html>
    <?php
}
