<?php
// 1. Load configurations
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/includes/bootstrap.php';

if (ob_get_length()) ob_clean();
header("Content-Type: application/xml; charset=utf-8");

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

    <!-- ── STATIC PAGES ── -->
    <url>
        <loc><?php echo rtrim(SITE_URL, '/') ?>/</loc>
        <priority>1.0</priority>
        <changefreq>monthly</changefreq>
    </url>
    <url>
        <loc><?php echo rtrim(SITE_URL, '/') ?>/about</loc>
        <priority>0.8</priority>
        <changefreq>monthly</changefreq>
    </url>
    <url>
        <loc><?php echo rtrim(SITE_URL, '/') ?>/blog</loc>
        <priority>0.9</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc><?php echo rtrim(SITE_URL, '/') ?>/projects</loc>
        <priority>0.9</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc><?php echo rtrim(SITE_URL, '/') ?>/contact</loc>
        <priority>0.7</priority>
        <changefreq>monthly</changefreq>
    </url>
    <url>
        <loc><?php echo rtrim(SITE_URL, '/') ?>/services</loc>
        <priority>0.7</priority>
        <changefreq>monthly</changefreq>
    </url>
    

    <?php
    try {
        $db = db();

        // ── BLOG POSTS ────────────────────────────────────────
        $postStmt = $db->query(
            "SELECT slug, updated_at
             FROM posts
             WHERE status = 'published'
             ORDER BY published_at DESC"
        );
        while ($post = $postStmt->fetch()) {
            $date = date('Y-m-d', strtotime($post['updated_at']));
            $url  = rtrim(SITE_URL, '/') . '/blog/' . $post['slug'];
            echo "    <url>\n";
            echo "        <loc>" . htmlspecialchars($url) . "</loc>\n";
            echo "        <lastmod>" . $date . "</lastmod>\n";
            echo "        <priority>0.6</priority>\n";
            echo "        <changefreq>monthly</changefreq>\n";
            echo "    </url>\n";
        }

        // ── PRODUCT LISTING PAGE PER CATEGORY ─────────────────
        // Each category anchor on /products is a crawlable URL worth indexing
        $catStmt = $db->query(
            "SELECT pc.slug, pc.updated_at
             FROM product_categories pc
             WHERE EXISTS (
                 SELECT 1 FROM products p
                 WHERE p.category_id = pc.id AND p.status = 'published'
             )
             ORDER BY pc.sort_order ASC"
        );
        while ($cat = $catStmt->fetch()) {
            // /products#oils — use the anchor form so Google can crawl sections
            $url = rtrim(SITE_URL, '/') . '/products#' . $cat['slug'];
            echo "    <url>\n";
            echo "        <loc>" . htmlspecialchars($url) . "</loc>\n";
            if (!empty($cat['updated_at'])) {
                echo "        <lastmod>" . date('Y-m-d', strtotime($cat['updated_at'])) . "</lastmod>\n";
            }
            echo "        <priority>0.7</priority>\n";
            echo "        <changefreq>weekly</changefreq>\n";
            echo "    </url>\n";
        }

        // ── INDIVIDUAL PRODUCT DETAIL PAGES ───────────────────
        $prodStmt = $db->query(
            "SELECT slug, updated_at
             FROM products
             WHERE status = 'published'
             ORDER BY updated_at DESC"
        );
        while ($prod = $prodStmt->fetch()) {
            $date = date('Y-m-d', strtotime($prod['updated_at']));
            $url  = rtrim(SITE_URL, '/') . '/products/' . $prod['slug'];
            echo "    <url>\n";
            echo "        <loc>" . htmlspecialchars($url) . "</loc>\n";
            echo "        <lastmod>" . $date . "</lastmod>\n";
            echo "        <priority>0.8</priority>\n";
            echo "        <changefreq>weekly</changefreq>\n";
            echo "    </url>\n";
        }
    } catch (Exception $e) {
        error_log("Sitemap DB Error: " . $e->getMessage());
    }
    ?>

</urlset>