<?php
require_once 'config/site_config.php';
header('Content-Type: application/xml; charset=UTF-8');
header('X-Robots-Tag: noindex');
header('Cache-Control: public, max-age=3600');

$today = date('Y-m-d');
$month = date('Y-m-d', strtotime('-30 days'));

// ── Helper: get file lastmod date ──────────────────────────────────────────
function file_lastmod(string $path): string {
    return file_exists($path) ? date('Y-m-d', filemtime($path)) : date('Y-m-d');
}

// ── Helper: scan a directory for .php files (skip templates/underscores) ──
function scan_php(string $dir): array {
    $files = [];
    foreach (glob($dir . '/*.php') as $f) {
        $base = basename($f, '.php');
        if (str_starts_with($base, '_')) continue; // skip _template files
        $files[] = ['file' => $f, 'slug' => $base];
    }
    return $files;
}

// ── Static core URLs ───────────────────────────────────────────────────────
$urls = [
    // Core
    ['loc' => SITE_URL . '/',              'lastmod' => file_lastmod(__DIR__ . '/index.php'),       'changefreq' => 'weekly',  'priority' => '1.0'],
    ['loc' => SITE_URL . '/features',      'lastmod' => file_lastmod(__DIR__ . '/features.php'),    'changefreq' => 'monthly', 'priority' => '0.9'],
    ['loc' => SITE_URL . '/pricing',       'lastmod' => file_lastmod(__DIR__ . '/pricing.php'),     'changefreq' => 'weekly',  'priority' => '0.9'],
    ['loc' => SITE_URL . '/how-it-works',  'lastmod' => file_lastmod(__DIR__ . '/how-it-works.php'),'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => SITE_URL . '/testimonials',  'lastmod' => file_lastmod(__DIR__ . '/testimonials.php'),'changefreq' => 'monthly', 'priority' => '0.7'],
    ['loc' => SITE_URL . '/blog',          'lastmod' => file_lastmod(__DIR__ . '/blog/index.php'),  'changefreq' => 'weekly',  'priority' => '0.8'],

    // Industry pages
    ['loc' => SITE_URL . '/free/retail-billing-software',       'lastmod' => file_lastmod(__DIR__ . '/free/retail-billing-software.php'),       'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => SITE_URL . '/free/kirana-store-billing-software', 'lastmod' => file_lastmod(__DIR__ . '/free/kirana-store-billing-software.php'), 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => SITE_URL . '/free/distributor-billing-software',  'lastmod' => file_lastmod(__DIR__ . '/free/distributor-billing-software.php'),  'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => SITE_URL . '/free/medical-shop-billing-software', 'lastmod' => file_lastmod(__DIR__ . '/free/medical-shop-billing-software.php'), 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => SITE_URL . '/free/cloth-shop-billing-software',   'lastmod' => file_lastmod(__DIR__ . '/free/cloth-shop-billing-software.php'),   'changefreq' => 'monthly', 'priority' => '0.8'],

    // Legal
    ['loc' => SITE_URL . '/partner', 'lastmod' => file_lastmod(__DIR__ . '/partner.php'), 'changefreq' => 'monthly', 'priority' => '0.6'],
    ['loc' => SITE_URL . '/careers', 'lastmod' => file_lastmod(__DIR__ . '/careers.php'), 'changefreq' => 'monthly', 'priority' => '0.6'],
    ['loc' => SITE_URL . '/privacy', 'lastmod' => file_lastmod(__DIR__ . '/privacy.php'), 'changefreq' => 'yearly', 'priority' => '0.4'],
    ['loc' => SITE_URL . '/terms',   'lastmod' => file_lastmod(__DIR__ . '/terms.php'),   'changefreq' => 'yearly', 'priority' => '0.4'],
    ['loc' => SITE_URL . '/refund',  'lastmod' => file_lastmod(__DIR__ . '/refund.php'),  'changefreq' => 'yearly', 'priority' => '0.4'],
];

// ── Auto-scan: Blog articles ───────────────────────────────────────────────
foreach (scan_php(__DIR__ . '/blog') as $entry) {
    if ($entry['slug'] === 'index') continue; // already added above
    $urls[] = [
        'loc'        => SITE_URL . '/blog/' . $entry['slug'],
        'lastmod'    => file_lastmod($entry['file']),
        'changefreq' => 'yearly',
        'priority'   => '0.7',
    ];
}

// ── Auto-scan: Free tools ──────────────────────────────────────────────────
foreach (scan_php(__DIR__ . '/tools') as $entry) {
    $urls[] = [
        'loc'        => SITE_URL . '/tools/' . $entry['slug'],
        'lastmod'    => file_lastmod($entry['file']),
        'changefreq' => 'monthly',
        'priority'   => '0.7',
    ];
}

// ── Auto-scan: Formats ─────────────────────────────────────────────────────
foreach (scan_php(__DIR__ . '/formats') as $entry) {
    $urls[] = [
        'loc'        => SITE_URL . '/formats/' . $entry['slug'],
        'lastmod'    => file_lastmod($entry['file']),
        'changefreq' => 'monthly',
        'priority'   => '0.6',
    ];
}

// ── Output XML ────────────────────────────────────────────────────────────
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' . "\n";
echo '        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' . "\n";
echo '        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9' . "\n";
echo '        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n";

foreach ($urls as $url) {
    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($url['loc'], ENT_XML1) . "</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>';
