<?php
/**
 * sitemap.xml
 * Dynamic XML Sitemap Generator for iPlug.lk
 * Lists all public-facing pages, categories, and products for search engine discovery.
 */

header('Content-Type: application/xml; charset=utf-8');

require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/config/database.php';

$baseUrl = APP_URL;

// Static pages with their priorities and change frequencies
$staticPages = [
    ['loc' => '/index.php',            'priority' => '1.00', 'changefreq' => 'daily'],
    ['loc' => '/shop.php',             'priority' => '0.90', 'changefreq' => 'daily'],
    ['loc' => '/about.php',            'priority' => '0.70', 'changefreq' => 'monthly'],
    ['loc' => '/contact.php',          'priority' => '0.70', 'changefreq' => 'monthly'],
    ['loc' => '/repair.php',           'priority' => '0.80', 'changefreq' => 'weekly'],
    ['loc' => '/repair-quote.php',     'priority' => '0.80', 'changefreq' => 'weekly'],
    ['loc' => '/faq.php',              'priority' => '0.60', 'changefreq' => 'monthly'],
    ['loc' => '/warranty.php',         'priority' => '0.60', 'changefreq' => 'monthly'],
    ['loc' => '/terms.php',            'priority' => '0.40', 'changefreq' => 'yearly'],
    ['loc' => '/privacy.php',          'priority' => '0.40', 'changefreq' => 'yearly'],
    ['loc' => '/order-tracking.php',   'priority' => '0.60', 'changefreq' => 'weekly'],
    ['loc' => '/favorites.php',        'priority' => '0.50', 'changefreq' => 'weekly'],
    ['loc' => '/blog.php',             'priority' => '0.85', 'changefreq' => 'daily'],
];

$urls = [];

// Add static pages
foreach ($staticPages as $page) {
    $urls[] = [
        'loc' => $baseUrl . $page['loc'],
        'priority' => $page['priority'],
        'changefreq' => $page['changefreq'],
    ];
}

// Fetch dynamic content from database
try {
    $db = Config\Database::connect();
    
    // Categories
    $cats = $db->query("SELECT slug, updated_at FROM categories ORDER BY id ASC")->fetchAll();
    foreach ($cats as $cat) {
        $urls[] = [
            'loc' => $baseUrl . '/shop.php?cat=' . urlencode($cat['slug']),
            'priority' => '0.85',
            'changefreq' => 'daily',
        ];
    }
    
    // Products
    $prods = $db->query("SELECT id, updated_at FROM products ORDER BY id DESC")->fetchAll();
    foreach ($prods as $prod) {
        $urls[] = [
            'loc' => $baseUrl . '/product.php?id=' . $prod['id'],
            'priority' => '0.80',
            'changefreq' => 'weekly',
        ];
    }
    
    // Blog Posts
    $blogPosts = $db->query("SELECT slug, updated_at FROM blog_posts WHERE status = 'published' ORDER BY id DESC")->fetchAll();
    foreach ($blogPosts as $bp) {
        $urls[] = [
            'loc' => $baseUrl . '/blog.php?slug=' . urlencode($bp['slug']),
            'priority' => '0.75',
            'changefreq' => 'monthly',
        ];
    }
} catch (Exception $e) {
    // Fallback — static pages only will be served
}

// Output XML
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<?php foreach ($urls as $url): ?>
    <url>
        <loc><?php echo htmlspecialchars($url['loc']); ?></loc>
        <changefreq><?php echo $url['changefreq']; ?></changefreq>
        <priority><?php echo $url['priority']; ?></priority>
    </url>
<?php endforeach; ?>
</urlset>
