<?php
/**
 * ============================================================
 * SITEMAP - SCHOOLS (Dynamic)
 * ============================================================
 * 
 * This file generates a sitemap of all active schools.
 * It should be regenerated periodically.
 * ============================================================
 */

header('Content-Type: application/xml; charset=utf-8');

require_once __DIR__ . '/config.php';

try {
    $db = getDB();
    
    // Get all active schools
    $stmt = $db->query("
        SELECT id, school_code, school_name, updated_at 
        FROM schools 
        WHERE is_active = 1 
        ORDER BY updated_at DESC
    ");
    $schools = $stmt->fetchAll();
    
    // Get all principals
    $principalStmt = $db->query("
        SELECT id, first_name, last_name, updated_at 
        FROM users 
        WHERE user_type = 'principal' AND is_active = 1
    ");
    $principals = $principalStmt->fetchAll();
    
} catch (Exception $e) {
    // Fallback to empty
    $schools = [];
    $principals = [];
}

?>
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.sitemaps.org/schemas/sitemap-image/1.1">

    <?php foreach ($schools as $school): ?>
    <url>
        <loc>https://supavassistant.supavsolutions.com/schools/<?php echo urlencode($school['school_code']); ?>/</loc>
        <lastmod><?php echo date('Y-m-d', strtotime($school['updated_at'])); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.4</priority>
        <image:image>
            <image:loc>https://supavassistant.supavsolutions.com/assets/images/school-placeholder.jpg</image:loc>
            <image:caption><?php echo htmlspecialchars($school['school_name']); ?></image:caption>
        </image:image>
    </url>
    <?php endforeach; ?>

    <?php foreach ($principals as $principal): ?>
    <url>
        <loc>https://supavassistant.supavsolutions.com/principals/<?php echo $principal['id']; ?>/</loc>
        <lastmod><?php echo date('Y-m-d', strtotime($principal['updated_at'])); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.3</priority>
    </url>
    <?php endforeach; ?>

</urlset>