<?php
/**
 * SAM DIGITAL SOLUTIONS - Dynamic Sitemap Generator
 */

define('SECURE_ACCESS', true);

require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/config/functions.php';

header('Content-Type: application/xml; charset=utf-8');

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"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    
    <!-- Static Pages -->
    <url>
        <loc><?php echo SITE_URL; ?>/</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.0</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/about</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/services</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/portfolio</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/blog</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/contact</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/payment</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/careers</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.7</priority>
    </url>
    
    <!-- Services -->
    <?php
    $services = db()->query("SELECT slug, updated_at FROM services WHERE is_active = 1")->fetchAll();
    foreach ($services as $service):
    ?>
    <url>
        <loc><?php echo SITE_URL; ?>/services/<?php echo $service['slug']; ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($service['updated_at'] ?? 'now')); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- Portfolio Items -->
    <?php
    $portfolio = db()->query("SELECT slug, updated_at FROM portfolio_items WHERE is_active = 1")->fetchAll();
    foreach ($portfolio as $item):
    ?>
    <url>
        <loc><?php echo SITE_URL; ?>/portfolio/<?php echo $item['slug']; ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($item['updated_at'] ?? 'now')); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- Blog Posts -->
    <?php
    $blog = db()->query("SELECT slug, updated_at FROM blog_posts WHERE status = 'published'")->fetchAll();
    foreach ($blog as $post):
    ?>
    <url>
        <loc><?php echo SITE_URL; ?>/blog/<?php echo $post['slug']; ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($post['updated_at'] ?? 'now')); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endforeach; ?>
    
</urlset>