<?php
declare(strict_types=1);

/**
 * XML site haritası — Google Search Console: Sitemap URL olarak bu dosyayı ekleyin.
 * Örnek: https://kocasinanlojistik.com/sitemap.php
 */

function sitemapBaseUrl(): string
{
    if (!empty($_SERVER['HTTP_HOST'])) {
        $https = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
            || (isset($_SERVER['SERVER_PORT']) && (int) $_SERVER['SERVER_PORT'] === 443)
            || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https');
        $scheme = $https ? 'https' : 'http';
        return rtrim($scheme . '://' . $_SERVER['HTTP_HOST'], '/');
    }

    return 'https://kocasinanlojistik.com';
}

function sitemapLastMod(string $file): string
{
    $path = __DIR__ . DIRECTORY_SEPARATOR . $file;
    if (is_file($path)) {
        return gmdate('Y-m-d', filemtime($path));
    }
    return gmdate('Y-m-d');
}

function sitemapXmlEscape(string $value): string
{
    return htmlspecialchars($value, ENT_XML1 | ENT_QUOTES, 'UTF-8');
}

$base = sitemapBaseUrl();

$urls = [
    [
        'path' => '/index.php',
        'file' => 'index.php',
        'changefreq' => 'weekly',
        'priority' => '1.0',
    ],
    [
        'path' => '/hakkimizda.php',
        'file' => 'hakkimizda.php',
        'changefreq' => 'monthly',
        'priority' => '0.8',
    ],
    [
        'path' => '/takip.php',
        'file' => 'takip.php',
        'changefreq' => 'monthly',
        'priority' => '0.8',
    ],
    [
        'path' => '/iletisim.php',
        'file' => 'iletisim.php',
        'changefreq' => 'monthly',
        'priority' => '0.7',
    ],
];

header('Content-Type: application/xml; charset=UTF-8');

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($urls as $entry): ?>
  <url>
    <loc><?= sitemapXmlEscape($base . $entry['path']) ?></loc>
    <lastmod><?= sitemapXmlEscape(sitemapLastMod($entry['file'])) ?></lastmod>
    <changefreq><?= sitemapXmlEscape($entry['changefreq']) ?></changefreq>
    <priority><?= sitemapXmlEscape($entry['priority']) ?></priority>
  </url>
<?php endforeach; ?>
</urlset>
