<?php
require_once 'config.php';

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

try {
    $pdo = new PDO(
        "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8mb4",
        DB_USER,
        DB_PASS,
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );
    
    $videos = $pdo->query("
        SELECT id, updated_at 
        FROM videos 
        WHERE status = 'active' 
        ORDER BY updated_at DESC
    ")->fetchAll(PDO::FETCH_ASSOC);
    
    $categories = $pdo->query("SELECT slug FROM categories")->fetchAll(PDO::FETCH_ASSOC);
    
} catch (PDOException $e) {
    die($e->getMessage());
}

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <!-- Homepage -->
    <url>
        <loc>https://teve.sk/</loc>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <!-- Trending -->
    <url>
        <loc>https://teve.sk/trending.php</loc>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    
    <!-- Categories -->
    <url>
        <loc>https://teve.sk/categories.php</loc>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    
    <?php foreach ($categories as $cat): ?>
    <url>
        <loc>https://teve.sk/categories.php?slug=<?php echo urlencode($cat['slug']); ?></loc>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- Videos -->
    <?php foreach ($videos as $video): ?>
    <url>
        <loc>https://teve.sk/watch.php?id=<?php echo $video['id']; ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($video['updated_at'])); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.6</priority>
    </url>
    <?php endforeach; ?>
</urlset>