<?php
// ============================================================================
// /sitemap.php -> Dynamic XML Sitemap for Search Engines & Google Search Console
// ============================================================================

require_once __DIR__ . '/api/config/db.php';

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

$baseUrl = 'https://shoplenca.com';
$today = date('Y-m-d');

// Static key routes
$staticRoutes = [
    ['loc' => '/', 'changefreq' => 'daily', 'priority' => '1.0'],
    ['loc' => '/category', 'changefreq' => 'daily', 'priority' => '0.9'],
    ['loc' => '/bookstore', 'changefreq' => 'daily', 'priority' => '0.8'],
    ['loc' => '/blog', 'changefreq' => 'weekly', 'priority' => '0.7'],
    ['loc' => '/about', 'changefreq' => 'monthly', 'priority' => '0.5'],
    ['loc' => '/contact', 'changefreq' => 'monthly', 'priority' => '0.5'],
    ['loc' => '/faq', 'changefreq' => 'monthly', 'priority' => '0.5'],
    ['loc' => '/terms', 'changefreq' => 'monthly', 'priority' => '0.4'],
    ['loc' => '/privacy', 'changefreq' => 'monthly', 'priority' => '0.4'],
];

// 1. Categories
$categories = db_rows($conn, "SELECT slug, updated_at FROM category ORDER BY id ASC");

// 2. Products
$products = db_rows($conn, "SELECT slug, updated_at FROM products WHERE instock > 0 ORDER BY id DESC LIMIT 5000");

// 3. Books
$books = db_rows($conn, "SELECT slug, updated_at FROM books WHERE status = 'published' ORDER BY id DESC LIMIT 1000");

// 4. Blogs
$blogs = db_rows($conn, "SELECT id, updated_at FROM blog_posts ORDER BY id DESC LIMIT 500");

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

  <!-- Core Static Pages -->
<?php foreach ($staticRoutes as $page): ?>
  <url>
    <loc><?= htmlspecialchars($baseUrl . $page['loc']) ?></loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq><?= $page['changefreq'] ?></changefreq>
    <priority><?= $page['priority'] ?></priority>
  </url>
<?php endforeach; ?>

  <!-- Category Pages -->
<?php foreach ($categories as $cat): ?>
  <url>
    <loc><?= htmlspecialchars($baseUrl . '/category?slug=' . urlencode($cat['slug'])) ?></loc>
    <lastmod><?= !empty($cat['updated_at']) ? date('Y-m-d', strtotime($cat['updated_at'])) : $today ?></lastmod>
    <changefreq>daily</changefreq>
    <priority>0.8</priority>
  </url>
<?php endforeach; ?>

  <!-- Product Pages -->
<?php foreach ($products as $prod): ?>
  <url>
    <loc><?= htmlspecialchars($baseUrl . '/product?slug=' . urlencode($prod['slug'])) ?></loc>
    <lastmod><?= !empty($prod['updated_at']) ? date('Y-m-d', strtotime($prod['updated_at'])) : $today ?></lastmod>
    <changefreq>daily</changefreq>
    <priority>0.9</priority>
  </url>
<?php endforeach; ?>

  <!-- Book Pages -->
<?php foreach ($books as $bk): ?>
  <url>
    <loc><?= htmlspecialchars($baseUrl . '/book?slug=' . urlencode($bk['slug'])) ?></loc>
    <lastmod><?= !empty($bk['updated_at']) ? date('Y-m-d', strtotime($bk['updated_at'])) : $today ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.7</priority>
  </url>
<?php endforeach; ?>

  <!-- Blog Article Pages -->
<?php foreach ($blogs as $bg): ?>
  <url>
    <loc><?= htmlspecialchars($baseUrl . '/blog?id=' . $bg['id']) ?></loc>
    <lastmod><?= !empty($bg['updated_at']) ? date('Y-m-d', strtotime($bg['updated_at'])) : $today ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.6</priority>
  </url>
<?php endforeach; ?>

</urlset>
