<?php
// sitemap.xml.php
require_once 'config.php';

header('Content-Type: application/xml; charset=utf-8');

// Get all active services
$services_query = "SELECT id, name, updated_at FROM services WHERE status = 'active' ORDER BY updated_at DESC";
$services_result = $conn->query($services_query);

// Get current date for lastmod
$current_date = date('Y-m-d');

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
    
    // Home page
    echo '<url>';
    echo '<loc>' . SITE_URL . '/index.php</loc>';
    echo '<lastmod>' . $current_date . '</lastmod>';
    echo '<changefreq>daily</changefreq>';
    echo '<priority>1.0</priority>';
    echo '</url>';
    
    // Services page
    echo '<url>';
    echo '<loc>' . SITE_URL . '/services.php</loc>';
    echo '<lastmod>' . $current_date . '</lastmod>';
    echo '<changefreq>daily</changefreq>';
    echo '<priority>0.9</priority>';
    echo '</url>';
    
    // Individual service pages
    while($service = $services_result->fetch_assoc()) {
        echo '<url>';
        echo '<loc>' . SITE_URL . '/order.php?service_id=' . $service['id'] . '</loc>';
        echo '<lastmod>' . date('Y-m-d', strtotime($service['updated_at'])) . '</lastmod>';
        echo '<changefreq>weekly</changefreq>';
        echo '<priority>0.8</priority>';
        echo '</url>';
    }
    
    // Static pages
    $static_pages = [
        'login.php' => 'weekly',
        'register.php' => 'weekly',
        'contact.php' => 'monthly',
        'about.php' => 'monthly',
        'privacy.php' => 'monthly',
        'terms.php' => 'monthly',
        'refund.php' => 'monthly',
        'sitemap.php' => 'monthly',
        'pricing.php' => 'monthly',
        'support.php' => 'weekly',
        'faq.php' => 'monthly',
        'dashboard.php' => 'daily',
        'wallet.php' => 'daily',
        'orders.php' => 'daily',
        'profile.php' => 'weekly'
    ];
    
    foreach($static_pages as $page => $freq) {
        echo '<url>';
        echo '<loc>' . SITE_URL . '/' . $page . '</loc>';
        echo '<lastmod>' . $current_date . '</lastmod>';
        echo '<changefreq>' . $freq . '</changefreq>';
        echo '<priority>0.7</priority>';
        echo '</url>';
    }

echo '</urlset>';
?>