<?php
header('Content-Type: application/xml; charset=utf-8');
require_once 'config/database.php';

$baseUrl = "https://" . $_SERVER['HTTP_HOST'];
$today = date('Y-m-d');

// Ambil paket aktif
$queryPackages = "SELECT id, updated_at FROM packages WHERE is_active = 1 ORDER BY id DESC";
$stmtPackages = $pdo->prepare($queryPackages);
$stmtPackages->execute();
$packages = $stmtPackages->fetchAll();

// Ambil artikel (jika ada)
$queryArticles = "SELECT slug, updated_at FROM articles WHERE is_published = 1 ORDER BY id DESC";
$stmtArticles = $pdo->prepare($queryArticles);
$stmtArticles->execute();
$articles = $stmtArticles->fetchAll();

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:video="http://www.google.com/schemas/sitemap-video/1.1"
        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">
    
    <!-- Halaman Utama -->
    <url>
        <loc><?php echo $baseUrl; ?>/</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
        <image:image>
            <image:loc><?php echo $baseUrl; ?>/assets/img/og-image.jpg</image:loc>
            <image:title>PT. ALFARUQ ANUGERAH UTAMA - Travel Umroh Terpercaya</image:title>
            <image:caption>Travel umroh resmi dengan izin PPIU Kemenag, paket harga hemat fasilitas terhormat</image:caption>
        </image:image>
    </url>
    
    <!-- Halaman Statis -->
    <url>
        <loc><?php echo $baseUrl; ?>/about.php</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    
    <url>
        <loc><?php echo $baseUrl; ?>/packages.php</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    
    <url>
        <loc><?php echo $baseUrl; ?>/gallery.php</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
    
    <url>
        <loc><?php echo $baseUrl; ?>/contact.php</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    
    <url>
        <loc><?php echo $baseUrl; ?>/testimonial-qna.php</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>
    
    <!-- Detail Paket -->
    <?php foreach ($packages as $package): 
        $packageLastMod = date('Y-m-d', strtotime($package['updated_at']));
    ?>
    <url>
        <loc><?php echo $baseUrl; ?>/package-detail.php?id=<?php echo $package['id']; ?></loc>
        <lastmod><?php echo $packageLastMod; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- Artikel -->
    <?php foreach ($articles as $article): 
        $articleLastMod = date('Y-m-d', strtotime($article['updated_at']));
    ?>
    <url>
        <loc><?php echo $baseUrl; ?>/article.php?slug=<?php echo $article['slug']; ?></loc>
        <lastmod><?php echo $articleLastMod; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    <?php endforeach; ?>
    
</urlset>