<?php
/**
 * Gabarit public (Bootstrap 5 + thème par variables CSS).
 * @var string $content
 * @var string $pageTitle
 */

use App\Core\Settings;
use App\Models\Gallery;
use App\Models\Page;

$siteTitle      = (string) Settings::get('site_title', 'DANIEL TIZIANI');
$tagline        = (string) Settings::get('tagline', '');
$galleriesLabel = (string) Settings::get('galleries_label', 'Galeries');
$navBreakpoint  = (int) Settings::get('nav_breakpoint', '992');
$lang           = (string) Settings::get('default_lang', 'fr');
$navGalleries   = Gallery::publishedList();
$showContact  = (string) Settings::get('contact_enabled', '0') === '1'
             && (string) Settings::get('menu_show_contact', '1') === '1';
$legal        = Page::isPublished('mentions-legales');
$seoNoindex   = (string) Settings::get('seo_noindex', '0') === '1';

$theme = [
    'bg'              => (string) Settings::get('theme_color_bg', '#e6e6e6'),
    'text'            => (string) Settings::get('theme_color_text', '#7a7a7a'),
    'gallery_title'   => (string) Settings::get('theme_color_gallery_title', '#5a5a5a'),
    'breadcrumb_link'    => (string) Settings::get('theme_color_breadcrumb_link', '#7a7a7a'),
    'breadcrumb_current' => (string) Settings::get('theme_color_breadcrumb_current', '#5a5a5a'),
    'link'            => (string) Settings::get('theme_color_link', '#337ab7'),
    'link_hover'      => (string) Settings::get('theme_color_link_hover', '#23527c'),
    'header_bg'       => (string) Settings::get('theme_color_header_bg', '#a6a8a9'),
    'header_border'   => (string) Settings::get('theme_color_header_border', '#8f9193'),
    'menu_text'       => (string) Settings::get('theme_color_menu_text', '#444444'),
    'accent'          => (string) Settings::get('theme_color_accent', '#e2041a'),
    'submenu_bg'      => (string) Settings::get('theme_color_submenu_bg', '#ffffff'),
    'submenu_hover_bg' => (string) Settings::get('theme_color_submenu_hover_bg', '#f4f4f4'),
    'footer_bg'       => (string) Settings::get('theme_color_footer_bg', '#a6a8a9'),
    'hover'           => (string) Settings::get('theme_color_hover', '#4e4e4e'),
];
?><!DOCTYPE html>
<html lang="<?= e($lang) ?>">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= e($pageTitle ?? $siteTitle) ?></title>
    <?php if (!empty($metaDescription)): ?>
    <meta name="description" content="<?= e($metaDescription) ?>">
    <?php endif; ?>
    <?php if ($seoNoindex): ?>
    <meta name="robots" content="noindex, nofollow">
    <?php elseif (!empty($metaRobots)): ?>
    <meta name="robots" content="<?= e($metaRobots) ?>">
    <?php endif; ?>
    <meta property="og:type" content="website">
    <meta property="og:site_name" content="<?= e($siteTitle) ?>">
    <meta property="og:title" content="<?= e($pageTitle ?? $siteTitle) ?>">
    <meta property="og:url" content="<?= e(current_url()) ?>">
    <?php if (!empty($metaDescription)): ?><meta property="og:description" content="<?= e($metaDescription) ?>"><?php endif; ?>
    <link rel="preload" href="<?= e(asset('vendor/fonts/open-sans/open-sans-latin-400.woff2')) ?>" as="font" type="font/woff2" crossorigin>
    <link rel="stylesheet" href="<?= e(asset('vendor/bootstrap/bootstrap.min.css')) ?>">
    <link rel="stylesheet" href="<?= e(asset('vendor/glightbox/glightbox.min.css')) ?>">
    <link rel="stylesheet" href="<?= e(asset('css/app.css')) ?>">
    <style>
        /* Thème injecté APRÈS app.css : ces :root surchargent les valeurs par défaut d'app.css. */
        :root{
            --color-bg: <?= e($theme['bg']) ?>;
            --color-text: <?= e($theme['text']) ?>;
            --color-gallery-title: <?= e($theme['gallery_title']) ?>;
            --color-breadcrumb-link: <?= e($theme['breadcrumb_link']) ?>;
            --color-breadcrumb-current: <?= e($theme['breadcrumb_current']) ?>;
            --color-link: <?= e($theme['link']) ?>;
            --color-link-hover: <?= e($theme['link_hover']) ?>;
            --color-header-bg: <?= e($theme['header_bg']) ?>;
            --color-header-border: <?= e($theme['header_border']) ?>;
            --color-menu-text: <?= e($theme['menu_text']) ?>;
            --color-accent: <?= e($theme['accent']) ?>;
            --color-submenu-bg: <?= e($theme['submenu_bg']) ?>;
            --color-submenu-hover-bg: <?= e($theme['submenu_hover_bg']) ?>;
            --color-footer-bg: <?= e($theme['footer_bg']) ?>;
            --color-hover: <?= e($theme['hover']) ?>;
        }
        @media (max-width: <?= $navBreakpoint ?>px) {
            .main-nav-h { display: none !important; }
            .menu-toggle { display: inline-flex !important; }
        }
    </style>
</head>
<body>
<header class="site-header" id="siteHeader">
    <div class="container header-inner">
        <a class="brand" href="<?= e(base_url('/')) ?>">
            <span class="brand-name"><?= e($siteTitle) ?></span>
            <?php if ($tagline !== ''): ?><span class="brand-tagline"><?= e($tagline) ?></span><?php endif; ?>
        </a>
        <nav class="main-nav-h" aria-label="Navigation principale">
            <ul>
                <li><a href="<?= e(base_url('/')) ?>">Accueil</a></li>
                <?php if (!empty($navGalleries)): ?>
                <li class="has-sub">
                    <a href="<?= e(base_url('/')) ?>"><?= e($galleriesLabel) ?><span class="caret" aria-hidden="true"></span></a>
                    <ul class="submenu">
                        <?php foreach ($navGalleries as $g): ?>
                            <li><a href="<?= e(base_url($g['slug'])) ?>"><?= e($g['title']) ?></a></li>
                        <?php endforeach; ?>
                    </ul>
                </li>
                <?php endif; ?>
                <?php if ($showContact): ?><li><a href="<?= e(base_url('contact')) ?>">Contact</a></li><?php endif; ?>
            </ul>
        </nav>
        <button class="menu-toggle" type="button" data-bs-toggle="offcanvas" data-bs-target="#mainMenu" aria-controls="mainMenu" aria-label="Ouvrir le menu">
            <span></span><span></span><span></span>
        </button>
    </div>
</header>

<div class="offcanvas offcanvas-end main-menu" tabindex="-1" id="mainMenu" aria-labelledby="mainMenuLabel" data-nav-breakpoint="<?= (int) $navBreakpoint ?>">
    <div class="offcanvas-header">
        <h2 class="offcanvas-title h6 m-0" id="mainMenuLabel">Menu</h2>
        <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Fermer"></button>
    </div>
    <div class="offcanvas-body">
        <nav class="main-nav">
            <ul>
                <li><a href="<?= e(base_url('/')) ?>">Accueil</a></li>
                <?php if (!empty($navGalleries)): ?>
                    <li class="nav-label"><?= e($galleriesLabel) ?></li>
                    <?php foreach ($navGalleries as $g): ?>
                        <li class="nav-sub"><a href="<?= e(base_url($g['slug'])) ?>"><?= e($g['title']) ?></a></li>
                    <?php endforeach; ?>
                <?php endif; ?>
                <?php if ($showContact): ?>
                    <li><a href="<?= e(base_url('contact')) ?>">Contact</a></li>
                <?php endif; ?>
            </ul>
        </nav>
    </div>
</div>

<main class="container page-main">
    <?= $content ?>
</main>

<footer class="site-footer">
    <?php
        // Galeries réparties en 4 colonnes (équilibrées).
        $footCols = array_chunk($navGalleries, (int) max(1, ceil(count($navGalleries) / 4)));
    ?>
    <div class="container footer-grid">
        <div class="footer-brand">
            <h3><?= e($siteTitle) ?></h3>
            <p class="footer-tagline"><?= e($tagline) ?></p>
        </div>
        <?php if (!empty($footCols)): ?>
        <div class="footer-works">
            <h3 class="footer-works-title"><?= e($galleriesLabel) ?>&nbsp;:</h3>
            <div class="footer-gallery-grid">
                <?php foreach ($footCols as $col): ?>
                <ul class="footer-links">
                    <?php foreach ($col as $g): ?>
                        <li><a href="<?= e(base_url($g['slug'])) ?>"><?= e($g['title']) ?></a></li>
                    <?php endforeach; ?>
                </ul>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>
    </div>
    <div class="container footer-bottom"><small>&copy; <?= date('Y') ?> <?= e($siteTitle) ?><?php if ($legal): ?><span class="footer-sep">|</span><a href="<?= e(base_url('mentions-legales')) ?>">Mentions légales</a><?php endif; ?></small></div>
</footer>

<script src="<?= e(asset('vendor/bootstrap/bootstrap.bundle.min.js')) ?>"></script>
<script src="<?= e(asset('vendor/glightbox/glightbox.min.js')) ?>"></script>
<script src="<?= e(asset('js/public.js')) ?>"></script>
</body>
</html>
