<?php
// sitemap.xml
header('Content-Type: application/xml; charset=utf-8');

// اتصال قاعدة البيانات
$servername = "localhost";
$username = "servaspace";
$password = "servaspace.com";
$dbname = "info_servaspace";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("فشل الاتصال بقاعدة البيانات: " . $conn->connect_error);
}

$siteUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]";

$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">' . "\n";

// الصفحة الرئيسية
$xml .= '<url>' . "\n";
$xml .= '  <loc>' . $siteUrl . '/</loc>' . "\n";
$xml .= '  <lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
$xml .= '  <changefreq>daily</changefreq>' . "\n";
$xml .= '  <priority>1.0</priority>' . "\n";
$xml .= '  <xhtml:link rel="alternate" hreflang="ar" href="' . $siteUrl . '/?lang=ar"/>' . "\n";
$xml .= '  <xhtml:link rel="alternate" hreflang="en" href="' . $siteUrl . '/?lang=en"/>' . "\n";
$xml .= '</url>' . "\n";

// الخدمات
$sql = "SELECT id, title_ar, title_en, image, updated_at FROM services WHERE status = 'published'";
$result = $conn->query($sql);
if ($result && $result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $slug_ar = preg_replace('/[^a-zA-Z0-9\-\p{Arabic}]/u', '-', $row['title_ar']);
        $slug_ar = strtolower(trim(preg_replace('/-+/', '-', $slug_ar), '-'));
        
        $xml .= '<url>' . "\n";
        $xml .= '  <loc>' . $siteUrl . '/?service=' . $slug_ar . '</loc>' . "\n";
        $xml .= '  <lastmod>' . date('Y-m-d', strtotime($row['updated_at'])) . '</lastmod>' . "\n";
        $xml .= '  <changefreq>weekly</changefreq>' . "\n";
        $xml .= '  <priority>0.8</priority>' . "\n";
        if(!empty($row['image'])) {
            $xml .= '  <image:image>' . "\n";
            $xml .= '    <image:loc>' . $siteUrl . '/' . $row['image'] . '</image:loc>' . "\n";
            $xml .= '    <image:title><![CDATA[' . $row['title_ar'] . ']]></image:title>' . "\n";
            $xml .= '  </image:image>' . "\n";
        }
        $xml .= '  <xhtml:link rel="alternate" hreflang="ar" href="' . $siteUrl . '/?service=' . $slug_ar . '&amp;lang=ar"/>' . "\n";
        $xml .= '  <xhtml:link rel="alternate" hreflang="en" href="' . $siteUrl . '/?service=' . $slug_ar . '&amp;lang=en"/>' . "\n";
        $xml .= '</url>' . "\n";
    }
}

// العروض
$sql = "SELECT id, title_ar, start_date, end_date FROM offers WHERE status = 'published'";
$result = $conn->query($sql);
if ($result && $result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $slug_ar = preg_replace('/[^a-zA-Z0-9\-\p{Arabic}]/u', '-', $row['title_ar']);
        $slug_ar = strtolower(trim(preg_replace('/-+/', '-', $slug_ar), '-'));
        
        $xml .= '<url>' . "\n";
        $xml .= '  <loc>' . $siteUrl . '/?offer=' . $slug_ar . '</loc>' . "\n";
        $xml .= '  <lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
        $xml .= '  <changefreq>weekly</changefreq>' . "\n";
        $xml .= '  <priority>0.9</priority>' . "\n";
        if(strtotime($row['start_date']) <= time() && strtotime($row['end_date']) >= time()) {
            $xml .= '  <news:news>' . "\n";
            $xml .= '    <news:publication>' . "\n";
            $xml .= '      <news:name>ServaSpace</news:name>' . "\n";
            $xml .= '      <news:language>ar</news:language>' . "\n";
            $xml .= '    </news:publication>' . "\n";
            $xml .= '    <news:publication_date>' . date('Y-m-d', strtotime($row['start_date'])) . '</news:publication_date>' . "\n";
            $xml .= '    <news:title><![CDATA[' . $row['title_ar'] . ']]></news:title>' . "\n";
            $xml .= '  </news:news>' . "\n";
        }
        $xml .= '</url>' . "\n";
    }
}

$xml .= '</urlset>';

file_put_contents(__DIR__ . '/sitemap.xml', $xml);
echo $xml;

$conn->close();
?>