<?php
// Session uyarılarını önlemek için output buffering başlat
ob_start();

// Veritabanı bağlantısı
require_once 'store/includes/config.php';

// XML header'ı gönder
header('Content-Type: application/xml; charset=UTF-8');

// XML declaration'ı yazdır
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">' . "\n";
echo '    ' . "\n";
echo '    <!-- Ana Sayfa -->' . "\n";
echo '    <url>' . "\n";
echo '        <loc>' . STORE_URL . '</loc>' . "\n";
echo '        <lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
echo '        <changefreq>daily</changefreq>' . "\n";
echo '        <priority>1.0</priority>' . "\n";
echo '    </url>' . "\n";
echo '    ' . "\n";
echo '    <!-- Kategoriler -->' . "\n";

// Kategorileri getir
$query = $db->prepare("
    SELECT slug, updated_at 
    FROM nc_categories 
    WHERE status = 1 
    ORDER BY name ASC
");
$query->execute();
$categories = $query->fetchAll(PDO::FETCH_ASSOC);

foreach ($categories as $category) {
    echo '    <url>' . "\n";
    echo '        <loc>' . STORE_URL . '/category/' . $category['slug'] . '</loc>' . "\n";
    echo '        <lastmod>' . date('Y-m-d', strtotime($category['updated_at'])) . '</lastmod>' . "\n";
    echo '        <changefreq>weekly</changefreq>' . "\n";
    echo '        <priority>0.8</priority>' . "\n";
    echo '    </url>' . "\n";
}

echo '    ' . "\n";
echo '    <!-- Markalar -->' . "\n";

// Markaları getir
$query = $db->prepare("
    SELECT slug, updated_at 
    FROM nc_brands 
    WHERE status = 1 
    ORDER BY name ASC
");
$query->execute();
$brands = $query->fetchAll(PDO::FETCH_ASSOC);

foreach ($brands as $brand) {
    echo '    <url>' . "\n";
    echo '        <loc>' . STORE_URL . '/brand/' . $brand['slug'] . '</loc>' . "\n";
    echo '        <lastmod>' . date('Y-m-d', strtotime($brand['updated_at'])) . '</lastmod>' . "\n";
    echo '        <changefreq>weekly</changefreq>' . "\n";
    echo '        <priority>0.7</priority>' . "\n";
    echo '    </url>' . "\n";
}

echo '    ' . "\n";
echo '    <!-- Ürünler -->' . "\n";

// Ürünleri getir
$query = $db->prepare("
    SELECT p.slug, p.updated_at, 
           MIN(CASE WHEN pm.sort_order = 1 THEN CONCAT('', pm.file_path_large) END) as image,
           b.name as brand_name,
           c.name as category_name
    FROM nc_products p
    LEFT JOIN nc_product_media pm ON p.id = pm.product_id
    LEFT JOIN nc_brands b ON p.brand_id = b.id
    LEFT JOIN nc_categories c ON p.category_id = c.id
    WHERE p.status = 1
    GROUP BY p.id
    ORDER BY p.updated_at DESC
    LIMIT 50000
");
$query->execute();
$products = $query->fetchAll(PDO::FETCH_ASSOC);

foreach ($products as $product) {
    $product_url = STORE_URL . '/product/' . $product['slug'];
    $image_url = '';
    if (!empty($product['image'])) {
        $file_path = str_replace('../uploads/media/', '', $product['image']);
        $image_url = STORE_URL . '/uploads/media/' . $file_path;
    }
    
    echo '    <url>' . "\n";
    echo '        <loc>' . $product_url . '</loc>' . "\n";
    echo '        <lastmod>' . date('Y-m-d', strtotime($product['updated_at'])) . '</lastmod>' . "\n";
    echo '        <changefreq>weekly</changefreq>' . "\n";
    echo '        <priority>0.6</priority>' . "\n";
    
    if (!empty($image_url)) {
        echo '        <image:image>' . "\n";
        echo '            <image:loc>' . $image_url . '</image:loc>' . "\n";
        echo '            <image:title>' . htmlspecialchars($product['brand_name'] . ' ' . $product['category_name']) . '</image:title>' . "\n";
        echo '            <image:caption>' . htmlspecialchars($product['brand_name'] . ' ' . $product['category_name'] . ' yedek parça') . '</image:caption>' . "\n";
        echo '        </image:image>' . "\n";
    }
    
    echo '    </url>' . "\n";
}

echo '</urlset>' . "\n";

// Output buffering'i kapat ve çıktıyı gönder
ob_end_flush();
?>