<?php
/**
 * XML Sitemap Generator for 9jaoncloud
 * Generates a dynamic sitemap for search engines
 * 
 * Access: http://localhost/9jaoncloud/sitemap.xml
 */

// Load configuration
require_once __DIR__ . '/includes/config.php';
require_once __DIR__ . '/includes/db_connect.php';

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

// Start XML
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 
                            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

<?php
// Base URL
$baseUrl = BASE_URL;
$today = date('Y-m-d');

// =====================================================
// STATIC PAGES
// =====================================================
$staticPages = [
    ['loc' => '/index.php', 'changefreq' => 'daily', 'priority' => '1.0'],
    ['loc' => '/about.php', 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => '/services.php', 'changefreq' => 'weekly', 'priority' => '0.9'],
    ['loc' => '/tools.php', 'changefreq' => 'weekly', 'priority' => '0.8'],
    ['loc' => '/blog.php', 'changefreq' => 'daily', 'priority' => '0.9'],
    ['loc' => '/careers.php', 'changefreq' => 'weekly', 'priority' => '0.7'],
    ['loc' => '/contact.php', 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => '/book.php', 'changefreq' => 'monthly', 'priority' => '0.7'],
    ['loc' => '/privacy.php', 'changefreq' => 'yearly', 'priority' => '0.3'],
    ['loc' => '/terms.php', 'changefreq' => 'yearly', 'priority' => '0.3'],
    ['loc' => '/cookies.php', 'changefreq' => 'yearly', 'priority' => '0.3'],
];

foreach ($staticPages as $page) {
    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($baseUrl . $page['loc'], ENT_XML1) . "</loc>\n";
    echo "    <lastmod>" . $today . "</lastmod>\n";
    echo "    <changefreq>" . $page['changefreq'] . "</changefreq>\n";
    echo "    <priority>" . $page['priority'] . "</priority>\n";
    echo "  </url>\n";
}

// =====================================================
// SERVICES (from database)
// =====================================================
try {
    $stmt = $pdo->query("SELECT slug, updated_at FROM services WHERE slug IS NOT NULL AND slug != ''");
    $services = $stmt->fetchAll();
    
    foreach ($services as $service) {
        $lastmod = $service['updated_at'] ? date('Y-m-d', strtotime($service['updated_at'])) : $today;
        echo "  <url>\n";
        echo "    <loc>" . htmlspecialchars($baseUrl . '/services/' . $service['slug'], ENT_XML1) . "</loc>\n";
        echo "    <lastmod>" . htmlspecialchars($lastmod) . "</lastmod>\n";
        echo "    <changefreq>weekly</changefreq>\n";
        echo "    <priority>0.8</priority>\n";
        echo "  </url>\n";
    }
} catch (PDOException $e) {
    error_log("Sitemap Services Error: " . $e->getMessage());
}

// =====================================================
// BLOG POSTS (from database)
// =====================================================
try {
    $stmt = $pdo->query("SELECT slug, created_at, updated_at FROM blog_posts WHERE slug IS NOT NULL AND slug != ''");
    $blogPosts = $stmt->fetchAll();
    
    foreach ($blogPosts as $post) {
        $lastmod = $post['updated_at'] ? date('Y-m-d', strtotime($post['updated_at'])) : 
                   ($post['created_at'] ? date('Y-m-d', strtotime($post['created_at'])) : $today);
        echo "  <url>\n";
        echo "    <loc>" . htmlspecialchars($baseUrl . '/blog/' . $post['slug'], ENT_XML1) . "</loc>\n";
        echo "    <lastmod>" . htmlspecialchars($lastmod) . "</lastmod>\n";
        echo "    <changefreq>monthly</changefreq>\n";
        echo "    <priority>0.7</priority>\n";
        echo "  </url>\n";
    }
} catch (PDOException $e) {
    error_log("Sitemap Blog Posts Error: " . $e->getMessage());
}

// =====================================================
// JOB LISTINGS (from database)
// =====================================================
try {
    $stmt = $pdo->query("SELECT id, title, created_at FROM jobs WHERE status = 'Open' ORDER BY created_at DESC");
    $jobs = $stmt->fetchAll();
    
    foreach ($jobs as $job) {
        $lastmod = $job['created_at'] ? date('Y-m-d', strtotime($job['created_at'])) : $today;
        $jobUrl = $baseUrl . '/apply.php?id=' . $job['id'];
        echo "  <url>\n";
        echo "    <loc>" . htmlspecialchars($jobUrl, ENT_XML1) . "</loc>\n";
        echo "    <lastmod>" . htmlspecialchars($lastmod) . "</lastmod>\n";
        echo "    <changefreq>weekly</changefreq>\n";
        echo "    <priority>0.6</priority>\n";
        echo "  </url>\n";
    }
} catch (PDOException $e) {
    error_log("Sitemap Jobs Error: " . $e->getMessage());
}
?>

</urlset>
