<?php
/**
 * GCCPricing.com — Dynamic XML Sitemap
 * Access at: /sitemap.xml
 */
define('GCCPRICING', true);
require_once __DIR__ . '/config/config.php';

header('Content-Type: application/xml; charset=utf-8');
header('Cache-Control: public, max-age=86400');

$materials  = Database::fetchAll("SELECT slug, updated_date FROM materials WHERE status='active' ORDER BY updated_date DESC");
$categories = Database::fetchAll("SELECT slug FROM material_categories WHERE status='active'");
$suppliers  = Database::fetchAll("SELECT slug, updated_at FROM suppliers WHERE status='active' AND slug IS NOT NULL AND slug != '' ORDER BY updated_at DESC LIMIT 500");
$today      = date('Y-m-d');
?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

    <!-- Static Pages -->
    <url><loc><?= SITE_URL ?>/</loc><lastmod><?= $today ?></lastmod><changefreq>daily</changefreq><priority>1.0</priority></url>
    <url><loc><?= SITE_URL ?>/materials</loc><lastmod><?= $today ?></lastmod><changefreq>daily</changefreq><priority>0.9</priority></url>
    <url><loc><?= SITE_URL ?>/compare</loc><lastmod><?= $today ?></lastmod><changefreq>weekly</changefreq><priority>0.85</priority></url>
    <url><loc><?= SITE_URL ?>/suppliers</loc><lastmod><?= $today ?></lastmod><changefreq>weekly</changefreq><priority>0.8</priority></url>
    <url><loc><?= SITE_URL ?>/boq</loc><lastmod><?= $today ?></lastmod><changefreq>monthly</changefreq><priority>0.8</priority></url>
    <url><loc><?= SITE_URL ?>/pricing</loc><lastmod><?= $today ?></lastmod><changefreq>monthly</changefreq><priority>0.7</priority></url>
    <url><loc><?= SITE_URL ?>/data-methodology</loc><lastmod><?= $today ?></lastmod><changefreq>monthly</changefreq><priority>0.6</priority></url>
    <url><loc><?= SITE_URL ?>/list-your-company</loc><lastmod><?= $today ?></lastmod><changefreq>monthly</changefreq><priority>0.6</priority></url>
    <url><loc><?= SITE_URL ?>/auth/register.php</loc><lastmod><?= $today ?></lastmod><changefreq>monthly</changefreq><priority>0.6</priority></url>

    <!-- GCC Country Price Hubs — HIGH PRIORITY SEO PAGES -->
    <?php foreach (GCC_COUNTRIES as $gcc_c): $gcc_slug = strtolower(str_replace(' ','-',$gcc_c)); ?>
    <url><loc><?= SITE_URL ?>/<?= $gcc_slug ?>-construction-prices</loc><lastmod><?= $today ?></lastmod><changefreq>weekly</changefreq><priority>0.95</priority></url>
    <?php endforeach; ?>

    <!-- Country-filtered material listings -->
    <?php foreach (GCC_COUNTRIES as $gcc_c): ?>
    <url><loc><?= SITE_URL ?>/materials?country=<?= urlencode($gcc_c) ?></loc><lastmod><?= $today ?></lastmod><changefreq>daily</changefreq><priority>0.85</priority></url>
    <?php endforeach; ?>

    <!-- Categories -->
    <?php foreach ($categories as $cat): ?>
    <url>
        <loc><?= SITE_URL ?>/category/<?= e($cat['slug']) ?></loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php endforeach; ?>

    <!-- Supplier Profile Pages -->
    <?php foreach ($suppliers as $sup): ?>
    <url>
        <loc><?= SITE_URL ?>/suppliers/<?= e($sup['slug']) ?></loc>
        <lastmod><?= $sup['updated_at'] ? date('Y-m-d', strtotime($sup['updated_at'])) : $today ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    <?php endforeach; ?>

    <!-- Material Compare Pages -->
    <?php foreach ($materials as $mat):
        $matSlug = $mat['slug'];
        $countryPairs = [['qatar','uae'],['qatar','saudi-arabia'],['qatar','oman'],['uae','saudi-arabia'],['saudi-arabia','oman'],['kuwait','bahrain']];
        foreach ($countryPairs as $pair):
    ?>
    <url>
        <loc><?= SITE_URL ?>/compare/<?= e($matSlug) ?>-price-<?= $pair[0] ?>-vs-<?= $pair[1] ?></loc>
        <lastmod><?= $mat['updated_date'] ? date('Y-m-d',strtotime($mat['updated_date'])) : $today ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.55</priority>
    </url>
    <?php endforeach; endforeach; ?>

    <!-- Material Detail Pages -->
    <?php foreach ($materials as $mat):
        $lastmod = $mat['updated_date'] ? date('Y-m-d', strtotime($mat['updated_date'])) : $today;
    ?>
    <url>
        <loc><?= SITE_URL ?>/materials/<?= e($mat['slug']) ?></loc>
        <lastmod><?= $lastmod ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.75</priority>
    </url>
    <?php foreach (GCC_COUNTRIES as $country):
        $cSlug = strtolower(str_replace(' ', '-', $country));
    ?>
    <url>
        <loc><?= SITE_URL ?>/materials/<?= e($mat['slug']) ?>-price-<?= $cSlug ?></loc>
        <lastmod><?= $lastmod ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.65</priority>
    </url>
    <?php endforeach; ?>
    <?php endforeach; ?>

</urlset>
