<?php
header('Content-Type: application/xml; charset=utf-8');
require_once 'config/config.php';
require_once 'config/database.php';

$pages = [
  ['url' => '', 'priority' => '1.0', 'freq' => 'weekly'],
  ['url' => 'about', 'priority' => '0.8', 'freq' => 'monthly'],
  ['url' => 'services', 'priority' => '0.9', 'freq' => 'monthly'],
  ['url' => 'services/erp-solutions', 'priority' => '0.9', 'freq' => 'monthly'],
  ['url' => 'services/web-development', 'priority' => '0.9', 'freq' => 'monthly'],
  ['url' => 'services/digital-marketing', 'priority' => '0.9', 'freq' => 'monthly'],
  ['url' => 'services/business-documentation', 'priority' => '0.9', 'freq' => 'monthly'],
  ['url' => 'services/design-branding', 'priority' => '0.9', 'freq' => 'monthly'],
  ['url' => 'portfolio', 'priority' => '0.8', 'freq' => 'monthly'],
  ['url' => 'blog', 'priority' => '0.8', 'freq' => 'daily'],
  ['url' => 'contact', 'priority' => '0.8', 'freq' => 'monthly'],
  ['url' => 'get-quote', 'priority' => '0.9', 'freq' => 'monthly'],
  ['url' => 'privacy-policy', 'priority' => '0.3', 'freq' => 'yearly'],
  ['url' => 'terms', 'priority' => '0.3', 'freq' => 'yearly'],
];

$today = date('Y-m-d');

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach($pages as $p): ?>
  <url>
    <loc><?php echo SITE_URL . '/' . $p['url']; ?></loc>
    <lastmod><?php echo $today; ?></lastmod>
    <changefreq><?php echo $p['freq']; ?></changefreq>
    <priority><?php echo $p['priority']; ?></priority>
  </url>
<?php endforeach; ?>
<?php
$db = getDB();
if ($db) {
    try {
        $stmt = $db->prepare("SELECT slug, updated_at FROM blog_posts WHERE status='published'");
        $stmt->execute();
        while ($row = $stmt->fetch()) {
            echo "  <url>\n";
            echo "    <loc>" . SITE_URL . "/blog/" . htmlspecialchars($row['slug']) . "</loc>\n";
            echo "    <lastmod>" . date('Y-m-d', strtotime($row['updated_at'])) . "</lastmod>\n";
            echo "    <changefreq>monthly</changefreq>\n";
            echo "    <priority>0.7</priority>\n";
            echo "  </url>\n";
        }
    } catch (Exception $e) {}
}
?>
</urlset>
