<?php
header('Content-Type: application/xml; charset=utf-8');

$host = 'localhost';
$db = 'alshatei_karim_it';
$user = 'alshatei_karim';
$pass = 'Karim2025#';

$conn = mysqli_connect($host, $user, $pass, $db);
mysqli_set_charset($conn, "utf8mb4");

$projects = [];
$result = mysqli_query($conn, "SELECT id, title, updated_at FROM projects ORDER BY id DESC");
if ($result) {
    while ($row = mysqli_fetch_assoc($result)) {
        $projects[] = $row;
    }
}

$xml = '<?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">

    <!-- الصفحة الرئيسية -->
    <url>
        <loc>https://karim-it.com/</loc>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>';

foreach ($projects as $project) {
    $xml .= '
    <url>
        <loc>https://karim-it.com/project.php?id=' . $project['id'] . '</loc>
        <lastmod>' . date('Y-m-d', strtotime($project['updated_at'])) . '</lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>';
}

$xml .= '
    <url>
        <loc>https://karim-it.com/login.php</loc>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>
    <url>
        <loc>https://karim-it.com/privacy.php</loc>
        <changefreq>yearly</changefreq>
        <priority>0.3</priority>
    </url>
    <url>
        <loc>https://karim-it.com/terms.php</loc>
        <changefreq>yearly</changefreq>
        <priority>0.3</priority>
    </url>
</urlset>';

echo $xml;
?>