<?php
require_once 'hiring-config.php';

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

$conn = getHiringDBConnection();
$jobs = [];

if ($conn) {
    try {
        $stmt = $conn->prepare("
            SELECT job_slug, created_at, updated_at 
            FROM jobs 
            WHERE is_active = 1 
            ORDER BY created_at DESC
        ");
        $stmt->execute();
        $jobs = $stmt->fetchAll();
    } catch (Exception $e) {
        error_log("Sitemap error: " . $e->getMessage());
    }
}

$base_url = SITE_URL;
$current_date = date('c');
?>
<?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"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    
    <!-- Homepage -->
    <url>
        <loc><?php echo $base_url; ?>/</loc>
        <lastmod><?php echo $current_date; ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <!-- Job listings page -->
    <url>
        <loc><?php echo $base_url; ?>/index.php</loc>
        <lastmod><?php echo $current_date; ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    
    <!-- Apply page -->
    <url>
        <loc><?php echo $base_url; ?>/apply.php</loc>
        <lastmod><?php echo $current_date; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    
    <!-- Individual job pages -->
    <?php foreach ($jobs as $job): 
        $lastmod = !empty($job['updated_at']) ? date('c', strtotime($job['updated_at'])) : date('c', strtotime($job['created_at']));
    ?>
    <url>
        <loc><?php echo $base_url; ?>/job-details.php?slug=<?php echo urlencode($job['job_slug']); ?></loc>
        <lastmod><?php echo $lastmod; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- Department pages -->
    <?php 
    $departments = ['production', 'quality-control', 'maintenance', 'warehouse', 'logistics', 'administration'];
    foreach ($departments as $dept):
    ?>
    <url>
        <loc><?php echo $base_url; ?>/index.php?department=<?php echo urlencode(ucwords(str_replace('-', ' ', $dept))); ?></loc>
        <lastmod><?php echo $current_date; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.6</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- Location pages -->
    <url>
        <loc><?php echo $base_url; ?>/index.php?location=Meerut+Plant</loc>
        <lastmod><?php echo $current_date; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.6</priority>
    </url>
    
    <!-- Job type pages -->
    <?php 
    $job_types = ['Full-time', 'Part-time', 'Contract'];
    foreach ($job_types as $type):
    ?>
    <url>
        <loc><?php echo $base_url; ?>/index.php?job_type=<?php echo urlencode($type); ?></loc>
        <lastmod><?php echo $current_date; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- Privacy policy -->
    <url>
        <loc><?php echo $base_url; ?>/privacy-policy.php</loc>
        <lastmod><?php echo $current_date; ?></lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.3</priority>
    </url>
    
    <!-- Terms of service -->
    <url>
        <loc><?php echo $base_url; ?>/terms.php</loc>
        <lastmod><?php echo $current_date; ?></lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.3</priority>
    </url>
    
</urlset>