<?php
/**
 * Senrix.pl — sitemap.xml
 * Wielojęzyczny, z hreflang, lastmod i stronami realizacji
 */
require_once __DIR__ . '/inc/config.php';
require_once __DIR__ . '/inc/locale.php';
require_once __DIR__ . '/inc/realizacje.php';

header('Content-Type: application/xml; charset=utf-8');
header('X-Robots-Tag: noindex'); // sitemap sam w sobie nie powinien być indeksowany

$langs = sr_available_langs();

// Mapa URL dla każdego języka
$url_maps = [];
foreach ($langs as $l) {
    $url_maps[$l] = sr_url_map($l);
}

// Strona + jej odpowiedniki w każdym języku
// Format: ['key' => klucz_sr_url lub null dla /, 'priority', 'freq', 'lastmod']
$today = date('Y-m-d');

$pages = [
    [
        'urls'     => ['pl'=>'/', 'en'=>'/', 'de'=>'/', 'it'=>'/'],
        'priority' => '1.0',
        'freq'     => 'weekly',
        'lastmod'  => $today,
    ],
    [
        'urls'     => [
            'pl' => '/realizacje',
            'en' => '/portfolio',
            'de' => '/referenzen',
            'it' => '/realizzazioni',
        ],
        'priority' => '0.9',
        'freq'     => 'weekly',
        'lastmod'  => $today,
    ],
    [
        'urls'     => [
            'pl' => '/projekty',
            'en' => '/projects',
            'de' => '/projekte',
            'it' => '/progetti',
        ],
        'priority' => '0.7',
        'freq'     => 'monthly',
        'lastmod'  => $today,
    ],
    [
        'urls'     => [
            'pl' => '/uslugi',
            'en' => '/services',
            'de' => '/leistungen',
            'it' => '/servizi',
        ],
        'priority' => '0.9',
        'freq'     => 'monthly',
        'lastmod'  => $today,
    ],
    [
        'urls'     => [
            'pl' => '/uslugi/strony-internetowe',
            'en' => '/services/websites',
            'de' => '/leistungen/webseiten',
            'it' => '/servizi/siti-web',
        ],
        'priority' => '0.8',
        'freq'     => 'monthly',
        'lastmod'  => $today,
    ],
    [
        'urls'     => [
            'pl' => '/uslugi/video-podcasty',
            'en' => '/services/video-podcasts',
            'de' => '/leistungen/video-podcasts',
            'it' => '/servizi/video-podcast',
        ],
        'priority' => '0.8',
        'freq'     => 'monthly',
        'lastmod'  => $today,
    ],
    [
        'urls'     => [
            'pl' => '/uslugi/grafika-copy',
            'en' => '/services/graphics-copy',
            'de' => '/leistungen/grafik-texte',
            'it' => '/servizi/grafica-testi',
        ],
        'priority' => '0.8',
        'freq'     => 'monthly',
        'lastmod'  => $today,
    ],
    [
        'urls'     => [
            'pl' => '/uslugi/organizacja-wydarzen',
            'en' => '/services/events-management',
            'de' => '/leistungen/eventmanagement',
            'it' => '/servizi/gestione-eventi',
        ],
        'priority' => '0.8',
        'freq'     => 'monthly',
        'lastmod'  => $today,
    ],
    [
        'urls'     => [
            'pl' => '/o-mnie',
            'en' => '/about',
            'de' => '/ueber-mich',
            'it' => '/chi-sono',
        ],
        'priority' => '0.7',
        'freq'     => 'monthly',
        'lastmod'  => $today,
    ],
    [
        'urls'     => [
            'pl' => '/kontakt',
            'en' => '/contact',
            'de' => '/de-kontakt',
            'it' => '/contatti',
        ],
        'priority' => '0.8',
        'freq'     => 'monthly',
        'lastmod'  => $today,
    ],
    [
        'urls'     => [
            'pl' => '/polityka-prywatnosci',
            'en' => '/privacy-policy',
            'de' => '/datenschutz',
            'it' => '/informativa-privacy',
        ],
        'priority' => '0.2',
        'freq'     => 'yearly',
        'lastmod'  => '2025-01-01',
    ],
];

// Dodaj pojedyncze realizacje
$realizacje = sr_get_realizacje();
foreach ($realizacje as $r) {
    $pages[] = [
        'urls'     => [
            'pl' => '/realizacje/' . $r['slug'],
            'en' => '/portfolio/'  . $r['slug'],
            'de' => '/referenzen/' . $r['slug'],
            'it' => '/realizzazioni/' . $r['slug'],
        ],
        'priority' => '0.7',
        'freq'     => 'monthly',
        'lastmod'  => $today,
    ];
}

// Dodaj pojedyncze projekty
require_once __DIR__ . '/inc/projekty.php';
$projekty = sr_get_projekty();
foreach ($projekty as $p) {
    $pages[] = [
        'urls'     => [
            'pl' => '/projekty/' . $p['slug'],
            'en' => '/projects/' . $p['slug'],
            'de' => '/projekte/' . $p['slug'],
            'it' => '/progetti/' . $p['slug'],
        ],
        'priority' => '0.7',
        'freq'     => 'monthly',
        'lastmod'  => $today,
    ];
}

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xhtml="http://www.w3.org/1999/xhtml">
<?php foreach ($pages as $page): ?>
  <url>
    <loc><?= htmlspecialchars(SITE_URL . $page['urls']['pl']) ?></loc>
    <?php foreach ($langs as $l): ?>
    <?php if (isset($page['urls'][$l])): ?>
    <xhtml:link rel="alternate" hreflang="<?= $l ?>"
                href="<?= htmlspecialchars(SITE_URL . $page['urls'][$l]) ?>"/>
    <?php endif; ?>
    <?php endforeach; ?>
    <xhtml:link rel="alternate" hreflang="x-default"
                href="<?= htmlspecialchars(SITE_URL . $page['urls']['pl']) ?>"/>
    <lastmod><?= $page['lastmod'] ?></lastmod>
    <changefreq><?= $page['freq'] ?></changefreq>
    <priority><?= $page['priority'] ?></priority>
  </url>
<?php endforeach; ?>
</urlset>
