<?php
    require_once __DIR__ . '/bootstrap.php';
    $pageTitle = $palabras['sitemap'] . ' - Hey Gara';
    $pageDesc = 'Sitemap of Hey Gara';
?>
<!DOCTYPE html>
<html lang="<?php echo $idioma; ?>">
<?php include __DIR__ . '/head_gara.php'; ?>
<body class="gara-page">
<?php include __DIR__ . '/nav_gara.php'; ?>

<div class="container-fluid mt-3 mb-5">
<?php
echo "<h1>" . htmlspecialchars($palabras['sitemap'], ENT_QUOTES, 'UTF-8') . " - Hey Gara</h1>";

$guias = obtenerGuias($conn, $idioma);

$eventSlugs = [];
try {
    $eventSlugs = $conn->query("SELECT slug FROM events WHERE active = TRUE AND slug IS NOT NULL AND slug <> '' ORDER BY slug")
        ->fetchAll(PDO::FETCH_COLUMN);
} catch (PDOException $e) {
    error_log('[sitemap-events] ' . $e->getMessage());
}

$esEn = ($idioma === 'en');
?>

<div class="row">

    <div class="col-md-4">
        <h3><?php echo $esEn ? 'Main Pages' : 'Páginas principales'; ?></h3>
        <a href="https://www.heygara.com/"><?php echo $esEn ? 'Plan your trip with AI' : 'Planifica tu viaje con IA'; ?></a><br>
        <a href="https://www.heygara.com/guias"><?php echo $esEn ? 'Things to do in Canary Islands' : 'Qué hacer en Canarias'; ?></a><br>
        <a href="https://www.heygara.com/eventos"><?php echo $esEn ? 'Events' : 'Eventos'; ?></a><br>
        <a href="https://www.heygara.com/faq">FAQ / Preguntas frecuentes</a><br>
        <a href="https://www.heygara.com/turismo-responsable"><?php echo $esEn ? 'Responsible Tourism' : 'Turismo responsable'; ?></a><br>
        <a href="https://www.heygara.com/acerca-de"><?php echo $esEn ? 'About Us' : 'Acerca de'; ?></a><br>
        <a href="https://www.heygara.com/contacto"><?php echo $esEn ? 'Contact' : 'Contacto'; ?></a><br>

        <h3 class="mt-4"><?php echo $esEn ? 'Legal' : 'Legal'; ?></h3>
        <a href="https://www.heygara.com/privacidad"><?php echo $esEn ? 'Privacy' : 'Privacidad'; ?></a><br>
        <a href="https://www.heygara.com/aviso-legal"><?php echo $esEn ? 'Legal notice' : 'Aviso legal'; ?></a><br>
        <a href="https://www.heygara.com/politica-cookies"><?php echo $esEn ? 'Cookie policy' : 'Política de cookies'; ?></a><br>
    </div>

    <div class="col-md-8">
        <?php if ($eventSlugs): ?>
            <h3><?php echo $esEn ? 'Events' : 'Eventos'; ?></h3>
            <div class="row">
            <?php
            $totalEventos = count($eventSlugs);
            $eventosPorColumna = (int) ceil($totalEventos / 2);
            $columnaAbierta = false;

            foreach ($eventSlugs as $index => $eventSlug) {
                if ($index % $eventosPorColumna === 0) {
                    if ($columnaAbierta) echo "</div>";
                    echo "<div class='col-md-6'>";
                    $columnaAbierta = true;
                }
                $slug = (string) $eventSlug;
                echo '<a href="https://www.heygara.com/evento/' . rawurlencode($slug) . '">'
                    . htmlspecialchars(str_replace('-', ' ', $slug), ENT_QUOTES, 'UTF-8')
                    . '</a><br>';
            }
            if ($columnaAbierta) echo "</div>";
            ?>
            </div>
        <?php endif; ?>
    </div>
</div>

<hr>

<div class="row mt-4">
    <div class="col-12">
        <h3><?php echo $esEn ? 'All Guides' : 'Todas las guías'; ?></h3>
        <div class="row">
        <?php
        $totalGuias = count($guias);

        if ($totalGuias > 0) {
            $guiasPorColumna = (int) ceil($totalGuias / 3);
            $columnaAbierta = false;

            foreach (array_values($guias) as $index => $guia_data) {
                if ($index % $guiasPorColumna === 0) {
                    if ($columnaAbierta) echo "</div>";
                    echo "<div class='col-md-4'>";
                    $columnaAbierta = true;
                }

                $guia_nombre = htmlspecialchars((string) $guia_data['titulo'], ENT_QUOTES, 'UTF-8');
                $slug_guia   = rawurlencode((string) $guia_data['slug']);

                echo "<a href='https://www.heygara.com/guia/{$slug_guia}'>{$guia_nombre}</a><br>";
            }
            if ($columnaAbierta) echo "</div>";
        }
        ?>
        </div>
    </div>
</div>

</div>
<?php
include __DIR__ . '/footer_gara.php';
?>
</body>
</html>