<?php
declare(strict_types=1);
require __DIR__ . '/includes/config.php';
require __DIR__ . '/includes/functions.php';
require __DIR__ . '/includes/i18n.php';
require __DIR__ . '/includes/data.php';
header('Content-Type: application/xml; charset=UTF-8');
$today = date('Y-m-d');

$pages = ['home', 'about', 'products', 'gallery', 'contact'];
$priorities = ['home' => '1.0', 'products' => '0.9', 'about' => '0.8', 'gallery' => '0.6', 'contact' => '0.7'];
$changefreq = ['home' => 'weekly', 'products' => 'weekly', 'about' => 'monthly', 'gallery' => 'monthly', 'contact' => 'monthly'];

$map = localized_route_map();

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">' . "\n";

$emit = function (string $loc, string $freq, string $prio, array $alts = []) use ($today) {
    echo "  <url>\n";
    echo "    <loc>" . e($loc) . "</loc>\n";
    echo "    <lastmod>{$today}</lastmod>\n";
    echo "    <changefreq>{$freq}</changefreq>\n";
    echo "    <priority>{$prio}</priority>\n";
    foreach ($alts as $hl => $href) {
        echo "    <xhtml:link rel=\"alternate\" hreflang=\"{$hl}\" href=\"" . e($href) . "\"/>\n";
    }
    echo "  </url>\n";
};

foreach ($pages as $page) {
    foreach (array_keys(LANGS) as $l) {
        $slug = null;
        foreach ($map[$l] as $s => $p) {
            if ($p === $page) {
                $slug = $s;
                break;
            }
        }
        if ($slug === null) continue;
        $path = $l === 'pl' ? $slug : '/' . $l . ($slug === '/' ? '' : $slug);
        $loc = SITE_URL . $path;

        $alternates = [];
        foreach (array_keys(LANGS) as $al) {
            $alSlug = null;
            foreach ($map[$al] as $s => $p) {
                if ($p === $page) { $alSlug = $s; break; }
            }
            if ($alSlug === null) continue;
            $alPath = $al === 'pl' ? $alSlug : '/' . $al . ($alSlug === '/' ? '' : $alSlug);
            $alternates[$al] = SITE_URL . $alPath;
        }
        $alternates['x-default'] = SITE_URL . ($page === 'home' ? '/' : (localized_route_map()['pl'][array_search($page, localized_route_map()['pl'], true) ?: '/'] ?? '/'));

        $emit($loc, $changefreq[$page], $priorities[$page], $alternates);
    }
}

$prodPrefix = [
    'pl' => '/produkty/', 'en' => '/en/products/', 'de' => '/de/produkte/',
    'it' => '/it/prodotti/', 'fr' => '/fr/produits/',
];
foreach (categories() as $c) {
    foreach (array_keys(LANGS) as $l) {
        $loc = SITE_URL . $prodPrefix[$l] . $c['slug'];
        $alts = [];
        foreach (array_keys(LANGS) as $al) {
            $alts[$al] = SITE_URL . $prodPrefix[$al] . $c['slug'];
        }
        $alts['x-default'] = SITE_URL . $prodPrefix['pl'] . $c['slug'];
        $emit($loc, 'monthly', '0.8', $alts);
    }
}
foreach (products() as $p) {
    foreach (array_keys(LANGS) as $l) {
        $loc = SITE_URL . $prodPrefix[$l] . $p['slug'];
        $alts = [];
        foreach (array_keys(LANGS) as $al) {
            $alts[$al] = SITE_URL . $prodPrefix[$al] . $p['slug'];
        }
        $alts['x-default'] = SITE_URL . $prodPrefix['pl'] . $p['slug'];
        $emit($loc, 'monthly', '0.7', $alts);
    }
}

echo '</urlset>';
