<?php
/**
 * SolvXYZ Dynamic XML Sitemap
 * Generates list of all public pages and products
 * Last updated: April 13, 2026
 */

require_once 'includes/session-config.php';
require_once 'config/database.php';
require_once 'includes/functions.php';

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"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
        xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">

    <!-- Homepage -->
    <url>
        <loc><?= getenv('SITE_URL') ?>/</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>

    <!-- Static Pages -->
    <url>
        <loc><?= getenv('SITE_URL') ?>/products.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>

    <url>
        <loc><?= getenv('SITE_URL') ?>/about.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>

    <url>
        <loc><?= getenv('SITE_URL') ?>/contact.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>

    <url>
        <loc><?= getenv('SITE_URL') ?>/faq.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>

    <url>
        <loc><?= getenv('SITE_URL') ?>/blog.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>

    <!-- Legal Pages -->
    <url>
        <loc><?= getenv('SITE_URL') ?>/privacy-policy.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.5</priority>
    </url>

    <url>
        <loc><?= getenv('SITE_URL') ?>/terms-of-service.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.5</priority>
    </url>

    <!-- Products -->
    <?php
    try {
        $stmt = $pdo->prepare("
            SELECT id, title, image, updated_at 
            FROM products 
            WHERE status = 'active' 
            ORDER BY updated_at DESC
        ");
        $stmt->execute();
        $products = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        foreach ($products as $product) {
            $updated = !empty($product['updated_at']) ? date('Y-m-d', strtotime($product['updated_at'])) : date('Y-m-d');
            ?>
    <url>
        <loc><?= getenv('SITE_URL') ?>/product.php?id=<?= $product['id'] ?></loc>
        <lastmod><?= $updated ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
        <image:image>
            <image:loc><?= getenv('SITE_URL') ?>/<?= htmlspecialchars($product['image']) ?></image:loc>
            <image:title><?= htmlspecialchars($product['title']) ?></image:title>
        </image:image>
    </url>
            <?php
        }
    } catch (Exception $e) {
        error_log("Sitemap product query error: " . $e->getMessage());
    }
    ?>

    <!-- Categories -->
    <?php
    try {
        $stmt = $pdo->prepare("
            SELECT id, name, updated_at 
            FROM categories 
            WHERE status = 'active' 
            ORDER BY name ASC
        ");
        $stmt->execute();
        $categories = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        foreach ($categories as $category) {
            $updated = !empty($category['updated_at']) ? date('Y-m-d', strtotime($category['updated_at'])) : date('Y-m-d');
            ?>
    <url>
        <loc><?= getenv('SITE_URL') ?>/products.php?category=<?= $category['id'] ?></loc>
        <lastmod><?= $updated ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
            <?php
        }
    } catch (Exception $e) {
        error_log("Sitemap category query error: " . $e->getMessage());
    }
    ?>

    <!-- Blog Posts -->
    <?php
    try {
        $stmt = $pdo->prepare("
            SELECT id, title, updated_at 
            FROM blog_posts 
            WHERE status = 'published' 
            ORDER BY updated_at DESC 
            LIMIT 50
        ");
        $stmt->execute();
        $posts = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        foreach ($posts as $post) {
            $updated = !empty($post['updated_at']) ? date('Y-m-d', strtotime($post['updated_at'])) : date('Y-m-d');
            ?>
    <url>
        <loc><?= getenv('SITE_URL') ?>/blog-post.php?id=<?= $post['id'] ?></loc>
        <lastmod><?= $updated ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
            <?php
        }
    } catch (Exception $e) {
        error_log("Sitemap blog query error: " . $e->getMessage());
    }
    ?>

</urlset>
