<?php
/**
 * SGN Solutions - Dynamic XML Sitemap
 */
require_once __DIR__ . '/config/config.php';

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

$pdo = getDBConnection();
$base = BASE_URL;

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <!-- Core Static Pages -->
  <url>
    <loc><?= $base ?>/index.php</loc>
    <priority>1.0</priority>
    <changefreq>weekly</changefreq>
  </url>
  <url>
    <loc><?= $base ?>/about.php</loc>
    <priority>0.8</priority>
    <changefreq>monthly</changefreq>
  </url>
  <url>
    <loc><?= $base ?>/services.php</loc>
    <priority>0.9</priority>
    <changefreq>weekly</changefreq>
  </url>
  <url>
    <loc><?= $base ?>/solutions.php</loc>
    <priority>0.9</priority>
    <changefreq>weekly</changefreq>
  </url>
  <url>
    <loc><?= $base ?>/portfolio.php</loc>
    <priority>0.8</priority>
    <changefreq>weekly</changefreq>
  </url>
  <url>
    <loc><?= $base ?>/team.php</loc>
    <priority>0.7</priority>
    <changefreq>monthly</changefreq>
  </url>
  <url>
    <loc><?= $base ?>/careers.php</loc>
    <priority>0.8</priority>
    <changefreq>weekly</changefreq>
  </url>
  <url>
    <loc><?= $base ?>/blog.php</loc>
    <priority>0.8</priority>
    <changefreq>daily</changefreq>
  </url>
  <url>
    <loc><?= $base ?>/contact.php</loc>
    <priority>0.8</priority>
    <changefreq>monthly</changefreq>
  </url>
  <url>
    <loc><?= $base ?>/faq.php</loc>
    <priority>0.6</priority>
    <changefreq>monthly</changefreq>
  </url>

  <!-- Dynamic Services -->
  <?php
    $services = $pdo->query("SELECT slug, updated_at FROM services WHERE is_active = 1")->fetchAll();
    foreach ($services as $s):
  ?>
  <url>
    <loc><?= $base ?>/service-detail.php?slug=<?= urlencode($s['slug']) ?></loc>
    <priority>0.85</priority>
    <lastmod><?= date('Y-m-d', strtotime($s['updated_at'] ?: 'now')) ?></lastmod>
    <changefreq>monthly</changefreq>
  </url>
  <?php endforeach; ?>

  <!-- Dynamic Solutions -->
  <?php
    $solutions = $pdo->query("SELECT slug, updated_at FROM solutions WHERE is_active = 1")->fetchAll();
    foreach ($solutions as $sol):
  ?>
  <url>
    <loc><?= $base ?>/solution-detail.php?slug=<?= urlencode($sol['slug']) ?></loc>
    <priority>0.85</priority>
    <lastmod><?= date('Y-m-d', strtotime($sol['updated_at'] ?: 'now')) ?></lastmod>
    <changefreq>monthly</changefreq>
  </url>
  <?php endforeach; ?>

  <!-- Dynamic Projects -->
  <?php
    $projects = $pdo->query("SELECT slug, updated_at FROM projects")->fetchAll();
    foreach ($projects as $p):
  ?>
  <url>
    <loc><?= $base ?>/project-detail.php?slug=<?= urlencode($p['slug']) ?></loc>
    <priority>0.75</priority>
    <lastmod><?= date('Y-m-d', strtotime($p['updated_at'] ?: 'now')) ?></lastmod>
    <changefreq>monthly</changefreq>
  </url>
  <?php endforeach; ?>

  <!-- Dynamic Blog Posts -->
  <?php
    $posts = $pdo->query("SELECT slug, updated_at FROM blog_posts WHERE status = 'published'")->fetchAll();
    foreach ($posts as $b):
  ?>
  <url>
    <loc><?= $base ?>/blog-detail.php?slug=<?= urlencode($b['slug']) ?></loc>
    <priority>0.7</priority>
    <lastmod><?= date('Y-m-d', strtotime($b['updated_at'] ?: 'now')) ?></lastmod>
    <changefreq>monthly</changefreq>
  </url>
  <?php endforeach; ?>

  <!-- Custom Dynamic Pages -->
  <?php
    $pages = $pdo->query("SELECT slug, updated_at FROM pages WHERE status = 'published'")->fetchAll();
    foreach ($pages as $pg):
  ?>
  <url>
    <loc><?= $base ?>/page.php?slug=<?= urlencode($pg['slug']) ?></loc>
    <priority>0.6</priority>
    <lastmod><?= date('Y-m-d', strtotime($pg['updated_at'] ?: 'now')) ?></lastmod>
    <changefreq>monthly</changefreq>
  </url>
  <?php endforeach; ?>
</urlset>
