<?php
/**
 * sitemap.php — Dynamic XML Sitemap
 * Serves as sitemap.xml via .htaccess rewrite
 * Auto-includes press items and updates lastmod dates
 */
header('Content-Type: application/xml; charset=utf-8');
header('Cache-Control: public, max-age=86400'); // cache 24hrs

$base    = 'https://ashish.au';
$dataDir = __DIR__ . '/data';
$today   = date('Y-m-d');

// Get last modified date of press.json for dynamic content
$pressFile  = $dataDir . '/press.json';
$photosFile = $dataDir . '/photos_meta.json';
$pressDate  = file_exists($pressFile)  ? date('Y-m-d', filemtime($pressFile))  : $today;
$photosDate = file_exists($photosFile) ? date('Y-m-d', filemtime($photosFile)) : $today;

// Core pages
$urls = [
    ['loc' => $base . '/',                        'lastmod' => $today,      'changefreq' => 'weekly',  'priority' => '1.0'],
    ['loc' => $base . '/pages/book.php',          'lastmod' => $today,      'changefreq' => 'monthly', 'priority' => '0.9'],
    ['loc' => $base . '/pages/credentials.php',   'lastmod' => $today,      'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => $base . '/pages/press.php',         'lastmod' => $pressDate,  'changefreq' => 'weekly',  'priority' => '0.8'],
    ['loc' => $base . '/pages/photos.php',        'lastmod' => $photosDate, 'changefreq' => 'weekly',  'priority' => '0.7'],
    ['loc' => $base . '/pages/testimonials.php',  'lastmod' => $today,      'changefreq' => 'monthly', 'priority' => '0.7'],
    ['loc' => $base . '/pages/privacy.php',       'lastmod' => $today,      'changefreq' => 'yearly',  'priority' => '0.3'],
    ['loc' => $base . '/pages/accessibility.php', 'lastmod' => $today,      'changefreq' => 'yearly',  'priority' => '0.3'],
];

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

foreach ($urls as $url) {
    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($url['loc']) . "</loc>\n";
    echo "    <lastmod>{$url['lastmod']}</lastmod>\n";
    echo "    <changefreq>{$url['changefreq']}</changefreq>\n";
    echo "    <priority>{$url['priority']}</priority>\n";
    echo "  </url>\n";
}

echo '</urlset>';
