<?php
// rss.xml - ملف RSS التلقائي بالكامل (لا يعتمد على ملفات خارجية)
header('Content-Type: application/rss+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);
}

// دالة لإنشاء slug
function createSlugForRSS($string) {
    $string = strip_tags($string);
    $string = preg_replace('/[^a-zA-Z0-9\-\p{Arabic}]/u', '-', $string);
    $string = preg_replace('/-+/', '-', $string);
    return strtolower(trim($string, '-'));
}

$siteUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]";

$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$xml .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">' . "\n";
$xml .= '<channel>' . "\n";
$xml .= '  <title><![CDATA[ServaSpace - الحلول الرقمية المتكاملة]]></title>' . "\n";
$xml .= '  <link>' . $siteUrl . '</link>' . "\n";
$xml .= '  <description><![CDATA[نقدم حلولاً رقمية متكاملة لتحويل أعمالك إلى المستقبل - تصميم مواقع، تطبيقات جوال، تسويق إلكتروني، استضافة سحابية]]></description>' . "\n";
$xml .= '  <language>ar</language>' . "\n";
$xml .= '  <lastBuildDate>' . date('D, d M Y H:i:s O') . '</lastBuildDate>' . "\n";
$xml .= '  <generator>ServaSpace RSS Generator</generator>' . "\n";
$xml .= '  <copyright>2025 ServaSpace. جميع الحقوق محفوظة</copyright>' . "\n";
$xml .= '  <webMaster>info@servaspace.com</webMaster>' . "\n";
$xml .= '  <category>تقنية</category>' . "\n";
$xml .= '  <category>تطوير مواقع</category>' . "\n";
$xml .= '  <category>تسويق إلكتروني</category>' . "\n";
$xml .= '  <atom:link href="' . $siteUrl . '/rss.xml" rel="self" type="application/rss+xml"/>' . "\n";
$xml .= '  <atom:link href="' . $siteUrl . '/rss_en.xml" rel="alternate" type="application/rss+xml" hreflang="en"/>' . "\n";
$xml .= '  <atom:link href="' . $siteUrl . '/rss.xml" rel="alternate" type="application/rss+xml" hreflang="ar"/>' . "\n";

// ========== إضافة الخدمات ==========
$sql_services = "SELECT id, title_ar, title_en, description_ar, description_en, image, updated_at FROM services WHERE status = 'published' ORDER BY id DESC LIMIT 50";
$result_services = $conn->query($sql_services);
if ($result_services && $result_services->num_rows > 0) {
    while($row = $result_services->fetch_assoc()) {
        $slug = createSlugForRSS($row['title_ar']);
        $xml .= '<item>' . "\n";
        $xml .= '  <title><![CDATA[خدمة: ' . htmlspecialchars($row['title_ar']) . ']]></title>' . "\n";
        $xml .= '  <link>' . $siteUrl . '/?service=' . $slug . '&amp;lang=ar</link>' . "\n";
        $xml .= '  <guid isPermaLink="true">' . $siteUrl . '/?service=' . $slug . '</guid>' . "\n";
        $xml .= '  <description><![CDATA[' . htmlspecialchars(substr($row['description_ar'], 0, 500)) . ']]></description>' . "\n";
        $xml .= '  <content:encoded><![CDATA[<p>' . htmlspecialchars($row['description_ar']) . '</p>]]></content:encoded>' . "\n";
        if(!empty($row['image'])) {
            $xml .= '  <media:content url="' . $siteUrl . '/' . $row['image'] . '" medium="image"/>' . "\n";
            $xml .= '  <enclosure url="' . $siteUrl . '/' . $row['image'] . '" type="image/jpeg"/>' . "\n";
        }
        $xml .= '  <pubDate>' . date('D, d M Y H:i:s O', strtotime($row['updated_at'])) . '</pubDate>' . "\n";
        $xml .= '  <category><![CDATA[خدمات رقمية]]></category>' . "\n";
        $xml .= '  <author>ServaSpace Team</author>' . "\n";
        $xml .= '</item>' . "\n";
    }
}

// ========== إضافة العروض ==========
$sql_offers = "SELECT id, title_ar, title_en, description_ar, description_en, image, start_date, end_date, updated_at FROM offers WHERE status = 'published' ORDER BY id DESC LIMIT 30";
$result_offers = $conn->query($sql_offers);
if ($result_offers && $result_offers->num_rows > 0) {
    while($row = $result_offers->fetch_assoc()) {
        $slug = createSlugForRSS($row['title_ar']);
        $xml .= '<item>' . "\n";
        $xml .= '  <title><![CDATA[عرض حصري: ' . htmlspecialchars($row['title_ar']) . ']]></title>' . "\n";
        $xml .= '  <link>' . $siteUrl . '/?offer=' . $slug . '&amp;lang=ar</link>' . "\n";
        $xml .= '  <guid isPermaLink="true">' . $siteUrl . '/?offer=' . $slug . '</guid>' . "\n";
        $xml .= '  <description><![CDATA[' . htmlspecialchars(substr($row['description_ar'], 0, 500)) . ']]></description>' . "\n";
        $xml .= '  <content:encoded><![CDATA[<p>' . htmlspecialchars($row['description_ar']) . '</p><p>ساري من: ' . $row['start_date'] . ' إلى: ' . $row['end_date'] . '</p>]]></content:encoded>' . "\n";
        if(!empty($row['image'])) {
            $xml .= '  <media:content url="' . $siteUrl . '/' . $row['image'] . '" medium="image"/>' . "\n";
        }
        $xml .= '  <pubDate>' . date('D, d M Y H:i:s O', strtotime($row['updated_at'])) . '</pubDate>' . "\n";
        $xml .= '  <category><![CDATA[عروض حصرية]]></category>' . "\n";
        $xml .= '</item>' . "\n";
    }
}

// ========== إضافة الشرائح (السلider) ==========
$sql_slides = "SELECT title_ar, description_ar, background FROM slides WHERE status = 'published' ORDER BY id DESC LIMIT 20";
$result_slides = $conn->query($sql_slides);
if ($result_slides && $result_slides->num_rows > 0) {
    while($row = $result_slides->fetch_assoc()) {
        $xml .= '<item>' . "\n";
        $xml .= '  <title><![CDATA[' . htmlspecialchars($row['title_ar']) . ']]></title>' . "\n";
        $xml .= '  <link>' . $siteUrl . '/#home</link>' . "\n";
        $xml .= '  <description><![CDATA[' . htmlspecialchars(substr($row['description_ar'], 0, 300)) . ']]></description>' . "\n";
        if(!empty($row['background'])) {
            $xml .= '  <media:content url="' . $siteUrl . '/' . $row['background'] . '" medium="image"/>' . "\n";
        }
        $xml .= '  <pubDate>' . date('D, d M Y H:i:s O') . '</pubDate>' . "\n";
        $xml .= '</item>' . "\n";
    }
}

// ========== إضافة الإحصائيات ==========
$sql_stats = "SELECT label_ar, number FROM statistics WHERE status = 'published'";
$result_stats = $conn->query($sql_stats);
if ($result_stats && $result_stats->num_rows > 0) {
    while($row = $result_stats->fetch_assoc()) {
        $xml .= '<item>' . "\n";
        $xml .= '  <title><![CDATA[إنجاز: ' . htmlspecialchars($row['label_ar']) . ' - ' . $row['number'] . ']]></title>' . "\n";
        $xml .= '  <link>' . $siteUrl . '/#statistics</link>' . "\n";
        $xml .= '  <description><![CDATA[نفخر بإنجازنا: ' . htmlspecialchars($row['label_ar']) . ' حيث وصلنا إلى ' . $row['number'] . ']]></description>' . "\n";
        $xml .= '  <pubDate>' . date('D, d M Y H:i:s O') . '</pubDate>' . "\n";
        $xml .= '</item>' . "\n";
    }
}

$xml .= '</channel>' . "\n";
$xml .= '</rss>';

// حفظ الملف وطباعته
file_put_contents(__DIR__ . '/rss.xml', $xml);
echo $xml;

$conn->close();
?>