<?php
// sitemap.xml - DYNAMIC SITEMAP
header('Content-Type: application/xml; charset=utf-8');
require_once 'includes/config.php';

$output = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"';
$output .= ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"';
$output .= ' xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"';
$output .= ' xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"';
$output .= ' xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"';
$output .= ' xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n";

// Homepage
$output .= '  <url>' . "\n";
$output .= '    <loc>https://mehlish.com/</loc>' . "\n";
$output .= '    <lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
$output .= '    <changefreq>daily</changefreq>' . "\n";
$output .= '    <priority>1.0</priority>' . "\n";
$output .= '  </url>' . "\n";

// Static pages
$static_pages = [
    'about' => 'about-us',
    'contact' => 'contact',
    'faq' => 'faq',
    'terms' => 'terms-and-conditions',
    'privacy' => 'privacy-policy',
    'shipping' => 'shipping-policy',
    'returns' => 'returns-policy',
    'blog' => 'blog',
    'products' => 'products',
    'categories' => 'categories',
    'new-arrivals' => 'new-arrivals',
    'sale' => 'sale',
    'clearance' => 'clearance',
    'gift-cards' => 'gift-cards',
    'stores' => 'stores',
    'careers' => 'careers',
    'press' => 'press',
    'affiliates' => 'affiliates',
    'wholesale' => 'wholesale',
    'custom' => 'custom-orders'
];

foreach ($static_pages as $page => $slug) {
    $output .= '  <url>' . "\n";
    $output .= '    <loc>https://mehlish.com/' . $slug . '</loc>' . "\n";
    $output .= '    <lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
    $output .= '    <changefreq>monthly</changefreq>' . "\n";
    $output .= '    <priority>0.8</priority>' . "\n";
    $output .= '  </url>' . "\n";
}

// Categories
$categories_query = "SELECT * FROM categories ORDER BY id DESC";
$categories_result = mysqli_query($conn, $categories_query);
while ($cat = mysqli_fetch_assoc($categories_result)) {
    $slug = strtolower(str_replace(' ', '-', preg_replace('/[^a-zA-Z0-9\s]/', '', $cat['cat_name'])));
    $output .= '  <url>' . "\n";
    $output .= '    <loc>https://mehlish.com/category/' . $cat['id'] . '/' . $slug . '</loc>' . "\n";
    $output .= '    <lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
    $output .= '    <changefreq>weekly</changefreq>' . "\n";
    $output .= '    <priority>0.9</priority>' . "\n";
    $output .= '  </url>' . "\n";
}

// Products
$products_query = "SELECT p.*, c.cat_name FROM products p 
                   LEFT JOIN categories c ON p.prod_cat = c.id 
                   ORDER BY p.id DESC";
$products_result = mysqli_query($conn, $products_query);
while ($product = mysqli_fetch_assoc($products_result)) {
    $slug = strtolower(str_replace(' ', '-', preg_replace('/[^a-zA-Z0-9\s]/', '', substr($product['prod_name'], 0, 50))));
    $output .= '  <url>' . "\n";
    $output .= '    <loc>https://mehlish.com/product/' . $product['id'] . '/' . $slug . '</loc>' . "\n";
    $output .= '    <lastmod>' . date('Y-m-d', strtotime($product['created_at'] ?? 'now')) . '</lastmod>' . "\n";
    $output .= '    <changefreq>weekly</changefreq>' . "\n";
    $output .= '    <priority>0.9</priority>' . "\n";
    
    // Product images
    $images_query = "SELECT * FROM prod_images WHERE p_random_number = '{$product['random_num']}'";
    $images_result = mysqli_query($conn, $images_query);
    while ($img = mysqli_fetch_assoc($images_result)) {
        $output .= '    <image:image>' . "\n";
        $output .= '      <image:loc>https://mehlish.com/uploads/products/' . $img['p_img'] . '</image:loc>' . "\n";
        $output .= '      <image:title>' . htmlspecialchars($product['prod_name']) . '</image:title>' . "\n";
        $output .= '      <image:caption>' . htmlspecialchars(substr($product['meta_description'] ?? '', 0, 200)) . '</image:caption>' . "\n";
        $output .= '    </image:image>' . "\n";
    }
    
    $output .= '  </url>' . "\n";
}

$output .= '</urlset>';

echo $output;
?>