<?php
        /**
        * Sitemap Generator
        * مولد خريطة الموقع لمحركات البحث
        */

        require_once 'includes/config.php';

        header('Content-Type: application/xml; charset=UTF-8');

        $sitemap = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
        $sitemap .= '<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">' . "\n";

    // Static pages
    $static_pages = [
    ['loc' => '', 'priority' => '1.0', 'changefreq' => 'daily'],
    ['loc' => 'about', 'priority' => '0.8', 'changefreq' => 'weekly'],
    ['loc' => 'contact', 'priority' => '0.8', 'changefreq' => 'weekly'],
    ['loc' => 'products', 'priority' => '0.9', 'changefreq' => 'daily'],
    ];

    foreach ($static_pages as $page) {
    $sitemap .= "\t<url>\n";
        $sitemap .= "\t\t<loc>" . SITE_URL . '/' . $page['loc'] . "</loc>\n";
        $sitemap .= "\t\t<lastmod>" . date('Y-m-d') . "</lastmod>\n";
        $sitemap .= "\t\t<changefreq>" . $page['changefreq'] . "</changefreq>\n";
        $sitemap .= "\t\t<priority>" . $page['priority'] . "</priority>\n";
        $sitemap .= "\t</url>\n";
    }

    // Products
    $products = getAllProducts(true);
    foreach ($products as $product) {
    $sitemap .= "\t<url>\n";
        $sitemap .= "\t\t<loc>" . SITE_URL . '/product/' . $product['slug'] . "</loc>\n";
        $sitemap .= "\t\t<lastmod>" . date('Y-m-d', strtotime($product['updated_at'])) . "</lastmod>\n";
        $sitemap .= "\t\t<changefreq>weekly</changefreq>\n";
        $sitemap .= "\t\t<priority>0.7</priority>\n";
        $sitemap .= "\t</url>\n";
    }

    $sitemap .= '</urlset>';

        echo $sitemap;
        ?>