<?php
/**
 * Dynamic XML Sitemap Generator for NeeYor Tech
 * Automatically aggregates canonical URLs, blog articles, courses, and services.
 */
require_once __DIR__ . '/config/db.php';

// Send XML headers
header('Content-Type: application/xml; charset=utf-8');

$siteBaseUrl = rtrim(defined('SITE_URL') ? SITE_URL : 'http://localhost/NEEYOR', '/');

// Build static canonical pages list
$staticPages = [
    [
        'loc' => $siteBaseUrl . '/',
        'changefreq' => 'daily',
        'priority' => '1.0'
    ],
    [
        'loc' => $siteBaseUrl . '/services.php',
        'changefreq' => 'weekly',
        'priority' => '0.95'
    ],
    [
        'loc' => $siteBaseUrl . '/assam-services.php',
        'changefreq' => 'weekly',
        'priority' => '0.95'
    ],
    [
        'loc' => $siteBaseUrl . '/html-sitemap.php',
        'changefreq' => 'weekly',
        'priority' => '0.80'
    ],
    [
        'loc' => $siteBaseUrl . '/courses.php',
        'changefreq' => 'weekly',
        'priority' => '0.9'
    ],
    [
        'loc' => $siteBaseUrl . '/projects.php',
        'changefreq' => 'weekly',
        'priority' => '0.8'
    ],
    [
        'loc' => $siteBaseUrl . '/about.php',
        'changefreq' => 'monthly',
        'priority' => '0.8'
    ],
    [
        'loc' => $siteBaseUrl . '/blog.php',
        'changefreq' => 'daily',
        'priority' => '0.85'
    ],
    [
        'loc' => $siteBaseUrl . '/contact.php',
        'changefreq' => 'monthly',
        'priority' => '0.8'
    ]
];

// Fetch dynamic blog posts
$blogPosts = [];
try {
    $db = getDB();
    $stmt = $db->query("SELECT id, title, slug, image, publish_date, created_at FROM blogs ORDER BY publish_date DESC");
    $blogPosts = $stmt->fetchAll();
} catch (Exception $e) {
    $blogPosts = [];
}

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"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

<?php foreach ($staticPages as $page): ?>
  <url>
    <loc><?php echo htmlspecialchars($page['loc']); ?></loc>
    <lastmod><?php echo date('Y-m-d'); ?></lastmod>
    <changefreq><?php echo $page['changefreq']; ?></changefreq>
    <priority><?php echo $page['priority']; ?></priority>
  </url>
<?php endforeach; ?>

<?php foreach ($blogPosts as $post): 
    $postUrl = $siteBaseUrl . '/blog-detail.php?slug=' . urlencode($post['slug']);
    $modDate = !empty($post['publish_date']) ? date('Y-m-d', strtotime($post['publish_date'])) : date('Y-m-d', strtotime($post['created_at']));
    $imgUrl = '';
    if (!empty($post['image'])) {
        $imgUrl = strpos($post['image'], 'http') === 0 ? $post['image'] : $siteBaseUrl . '/' . ltrim($post['image'], '/');
    }
?>
  <url>
    <loc><?php echo htmlspecialchars($postUrl, ENT_XML1 | ENT_QUOTES, 'UTF-8'); ?></loc>
    <lastmod><?php echo htmlspecialchars($modDate, ENT_XML1 | ENT_QUOTES, 'UTF-8'); ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.75</priority>
    <?php if ($imgUrl): ?>
    <image:image>
      <image:loc><?php echo htmlspecialchars($imgUrl, ENT_XML1 | ENT_QUOTES, 'UTF-8'); ?></image:loc>
      <image:title><?php echo htmlspecialchars(strip_tags($post['title']), ENT_XML1 | ENT_QUOTES, 'UTF-8'); ?></image:title>
    </image:image>
    <?php endif; ?>
  </url>
<?php endforeach; ?>

</urlset>
