<?php
include "config/database.php";

header("Content-Type: application/xml; charset=UTF-8");

$baseUrl = "https://photostoriesclub.gr";

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

    <url>
        <loc><?php echo $baseUrl; ?>/</loc>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>

    <url>
        <loc><?php echo $baseUrl; ?>/gallery.php</loc>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>

    

    <url>
        <loc><?php echo $baseUrl; ?>/contact.php</loc>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>

    <url>
        <loc><?php echo $baseUrl; ?>/privacy.php</loc>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>

    <url>
        <loc><?php echo $baseUrl; ?>/rules.php</loc>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>

<?php
$stmtPhotos = $conn->prepare("
    SELECT id, created_at
    FROM photos
    WHERE approved = 1
    ORDER BY created_at DESC
");
$stmtPhotos->execute();
$photos = $stmtPhotos->get_result();

while ($photo = $photos->fetch_assoc()):
?>
    <url>
        <loc><?php echo $baseUrl; ?>/photo.php?id=<?php echo (int)$photo['id']; ?></loc>
        <lastmod><?php echo date('c', strtotime($photo['created_at'])); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
<?php endwhile; ?>

<?php
$stmtUsers = $conn->prepare("
    SELECT id, created_at
    FROM users
    ORDER BY created_at DESC
");
$stmtUsers->execute();
$users = $stmtUsers->get_result();

while ($user = $users->fetch_assoc()):
?>
    <url>
        <loc><?php echo $baseUrl; ?>/profile.php?id=<?php echo (int)$user['id']; ?></loc>
        <lastmod><?php echo date('c', strtotime($user['created_at'])); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
<?php endwhile; ?>

</urlset>