<?php
// =============================
// sitemap.php — FINAL NO-DUPLICATES VERSION
// =============================

// 1. No warnings
ini_set('display_errors', 0);
error_reporting(E_ALL);

// 2. Load config
ob_start();
require __DIR__ . '/config/config.php';
ob_clean();

if (!defined('base_url')) {
    die("Missing base_url in config.php");
}

header('Content-Type: application/xml; charset=UTF-8');

// Normalize base URL
$BASE = rtrim(base_url, '/') . '/';

// =============================
// BLACKLIST — Must skip these
// =============================
$SKIP_SLUGS = [
    'categories', 'pages', 'thankyou', 'thank-you', 'success', 'failed',
    'checkout', 'search', 'error', '404', 'yatra', 'makhana-yatra'
];

function shouldSkip($slug, $skipList)
{
    $slug = trim($slug, '/');
    return in_array($slug, $skipList);
}

// =============================
// GLOBAL REGISTER (NO DUPLICATES)
// =============================
$ADDED_URLS = [];

// =============================
// Helper: Add URL if not duplicate
// =============================
function addSitemapUrl($loc, $lastmod = null, $priority = '0.80', $changefreq = 'weekly')
{
    global $ADDED_URLS;

    // Normalize URL
    $loc = rtrim($loc, '/');

    // Duplicate check
    if (in_array($loc, $ADDED_URLS)) {
        return;
    }

    // Mark as added
    $ADDED_URLS[] = $loc;

    // Output XML node
    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($loc, ENT_XML1, 'UTF-8') . "</loc>\n";

    if (!empty($lastmod)) {
        $ts = strtotime($lastmod);
        if ($ts !== false) {
            echo "    <lastmod>" . date('Y-m-d', $ts) . "</lastmod>\n";
        }
    }

    echo "    <changefreq>$changefreq</changefreq>\n";
    echo "    <priority>$priority</priority>\n";
    echo "  </url>\n";
}

// =============================
// START XML
// =============================
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ';
echo 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ';
echo 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 ';
echo 'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n";

// =============================
// STATIC PAGES
// =============================
$static = [
    '', // home
    'about-us', 'products', 'gallery', 'exhibition',
    'blog', 'contact-us', 'privacy-policy',
    'terms-conditions', 'partner', 'career',
    'video-gallery'
];

foreach ($static as $slug) {
    $url = $BASE . $slug;
    addSitemapUrl($url, date('Y-m-d'), '0.90', 'monthly');
}

// =============================
// DYNAMIC PAGES
// =============================

// -------- PAGES TABLE --------
$res = mysqli_query($conn, "SELECT slug, created_at FROM page WHERE status=1 ORDER BY created_at DESC");
while ($row = mysqli_fetch_assoc($res)) {

    $slug = trim($row['slug'], '/');
    if (shouldSkip($slug, $SKIP_SLUGS)) continue;

    addSitemapUrl($BASE . $slug, $row['created_at'], '0.85');
}

// -------- PRODUCTS --------
$res = mysqli_query($conn, "SELECT slug, created_at FROM products WHERE status=1 ORDER BY created_at DESC");
while ($row = mysqli_fetch_assoc($res)) {

    $slug = trim($row['slug'], '/');
    addSitemapUrl($BASE . 'product/' . $slug, $row['created_at'], '0.85', 'weekly');
}

// -------- BLOG POSTS --------
$res = mysqli_query($conn, "SELECT slug, date FROM post WHERE status=1 ORDER BY date DESC");
while ($row = mysqli_fetch_assoc($res)) {

    $slug = trim($row['slug'], '/');
    if (shouldSkip($slug, $SKIP_SLUGS)) continue;

    addSitemapUrl($BASE . 'blog/' . $slug, $row['date'], '0.85');
}

// -------- BLOG CATEGORIES --------
$res = mysqli_query($conn, "SELECT slug, created_at FROM post_categories WHERE status=1 ORDER BY created_at DESC");
while ($row = mysqli_fetch_assoc($res)) {

    $slug = trim($row['slug'], '/');
    if (shouldSkip($slug, $SKIP_SLUGS)) continue;

    addSitemapUrl($BASE . 'blog-category/' . $slug, $row['created_at']);
}

// -------- META TAG CUSTOM PAGES --------
$res = mysqli_query($conn, "SELECT slug, created_at FROM meta_tags ORDER BY created_at DESC");
while ($row = mysqli_fetch_assoc($res)) {

    $slug = trim($row['slug'], '/');

    if ($slug === '') continue;
    if (shouldSkip($slug, $SKIP_SLUGS)) continue;

    addSitemapUrl($BASE . $slug, $row['created_at'], '0.75');
}

echo "</urlset>";

mysqli_close($conn);
