<?php
    include "Admin/conn.php";
    header("Content-Type: application/xml; charset=utf-8");
    echo '<?xml version="1.0" encoding="UTF-8"?>';
    echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">';

    $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $base_url = $protocol.$_SERVER['HTTP_HOST'];

    // Static pages
    $pages = array("index", "about", "contact", "our-products", "partner", "blog", "service", "privacy", "terms", "cookie-policy");
    foreach ($pages as $page) {
        // Skip empty pages
        if (empty($page)) continue;
        
        // Build the clean URL structure
        $url_en = $base_url . "/en/" . ($page === "index" ? "" : $page . "/");
        $url_ar = $base_url . "/ar/" . ($page === "index" ? "" : $page . "/");
        
        echo "<url>";
        echo "<loc>".$url_en."</loc>";
        echo '<xhtml:link rel="alternate" hreflang="en" href="'.$url_en.'" />';
        echo '<xhtml:link rel="alternate" hreflang="ar" href="'.$url_ar.'" />';
        echo "</url>";
    }

    // Dynamic pages - Products (using product-details.php)
    $products_query = "SELECT id, name, name_ar FROM products ORDER BY id DESC";
    $products_result = mysqli_query($con, $products_query);
    if ($products_result) {
        while ($product = mysqli_fetch_assoc($products_result)) {
            $url_en = $base_url . "/en/product-details?id=" . $product['id'];
            $url_ar = $base_url . "/ar/product-details?id=" . $product['id'];
            
            echo "<url>";
            echo "<loc>".$url_en."</loc>";
            echo '<xhtml:link rel="alternate" hreflang="en" href="'.$url_en.'" />';
            echo '<xhtml:link rel="alternate" hreflang="ar" href="'.$url_ar.'" />';
            echo "</url>";
        }
    }

    // Dynamic pages - Blog posts (using blog-details.php)
    $blog_query = "SELECT id, title FROM blog ORDER BY id DESC";
    $blog_result = mysqli_query($con, $blog_query);
    if ($blog_result) {
        while ($blog = mysqli_fetch_assoc($blog_result)) {
            $url_en = $base_url . "/en/blog-details?id=" . $blog['id'];
            $url_ar = $base_url . "/ar/blog-details?id=" . $blog['id'];
            
            echo "<url>";
            echo "<loc>".$url_en."</loc>";
            echo '<xhtml:link rel="alternate" hreflang="en" href="'.$url_en.'" />';
            echo '<xhtml:link rel="alternate" hreflang="ar" href="'.$url_ar.'" />';
            echo "</url>";
        }
    }

    // Dynamic pages - Services (using single-service.php)
    $services_query = "SELECT id, title FROM services ORDER BY id DESC";
    $services_result = mysqli_query($con, $services_query);
    if ($services_result) {
        while ($service = mysqli_fetch_assoc($services_result)) {
            $url_en = $base_url . "/en/single-service?id=" . $service['id'];
            $url_ar = $base_url . "/ar/single-service?id=" . $service['id'];
            
            echo "<url>";
            echo "<loc>".$url_en."</loc>";
            echo '<xhtml:link rel="alternate" hreflang="en" href="'.$url_en.'" />';
            echo '<xhtml:link rel="alternate" hreflang="ar" href="'.$url_ar.'" />';
            echo "</url>";
        }
    }

    // Blog categories
    $blog_categories_query = "SELECT DISTINCT category FROM blog WHERE category IS NOT NULL AND category != '' ORDER BY category";
    $blog_categories_result = mysqli_query($con, $blog_categories_query);
    if ($blog_categories_result) {
        while ($category = mysqli_fetch_assoc($blog_categories_result)) {
            $category_name = $category['category'];
            $url_en = $base_url . "/en/blog-category?category=" . urlencode($category_name);
            $url_ar = $base_url . "/ar/blog-category?category=" . urlencode($category_name);
            
            echo "<url>";
            echo "<loc>".$url_en."</loc>";
            echo '<xhtml:link rel="alternate" hreflang="en" href="'.$url_en.'" />';
            echo '<xhtml:link rel="alternate" hreflang="ar" href="'.$url_ar.'" />';
            echo "</url>";
        }
    }

    // Product categories
    $product_categories_query = "SELECT id, name FROM categories ORDER BY id DESC";
    $product_categories_result = mysqli_query($con, $product_categories_query);
    if ($product_categories_result) {
        while ($category = mysqli_fetch_assoc($product_categories_result)) {
            $url_en = $base_url . "/en/product-category?category_id=" . $category['id'];
            $url_ar = $base_url . "/ar/product-category?category_id=" . $category['id'];
            
            echo "<url>";
            echo "<loc>".$url_en."</loc>";
            echo '<xhtml:link rel="alternate" hreflang="en" href="'.$url_en.'" />';
            echo '<xhtml:link rel="alternate" hreflang="ar" href="'.$url_ar.'" />';
            echo "</url>";
        }
    }



    echo '</urlset>';
?>