<?php
require_once __DIR__ . '/config/secure_public.php';

// Header
$urls = [];

// Tambahkan halaman statis penting
$urls[] = ['loc' => base_url('index.php'), 'priority' => '1.0', 'changefreq' => 'daily'];
$urls[] = ['loc' => base_url('profil'), 'priority' => '0.9', 'changefreq' => 'monthly'];
$urls[] = ['loc' => base_url('kegiatan.php'), 'priority' => '0.8', 'changefreq' => 'weekly'];
$urls[] = ['loc' => base_url('berita.php'), 'priority' => '0.8', 'changefreq' => 'weekly'];
$urls[] = ['loc' => base_url('dokumen.php'), 'priority' => '0.7', 'changefreq' => 'monthly'];

// Tambah semua profiles
$stmt = $pdo->query("SELECT slug, updated_at FROM profiles WHERE status='active'");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $urls[] = [
        'loc' => base_url('profil/' . $row['slug']),
        'priority' => '0.9',
        'changefreq' => 'monthly',
        'lastmod' => date('Y-m-d', strtotime($row['updated_at']))
    ];
}

// Build XML
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset/>');
$xml->addAttribute('xmlns','http://www.sitemaps.org/schemas/sitemap/0.9');

foreach ($urls as $u) {
    $url = $xml->addChild('url');
    $url->addChild('loc', htmlspecialchars($u['loc']));
    if (!empty($u['lastmod'])) $url->addChild('lastmod', $u['lastmod']);
    $url->addChild('changefreq', $u['changefreq']);
    $url->addChild('priority', $u['priority']);
}

$xml->asXML(__DIR__ . '/../sitemap.xml');
echo "Sitemap generated at /sitemap.xml\n";
