<?php
// Set XML header
header('Content-Type: application/xml; charset=utf-8');

// Database connection
include "includes/options.php";

$linkz = mysqli_connect($mysql_server, $mysql_user, $mysql_password, $mysql_database);
if (!$linkz) {
    die('Could not connect: ' . mysqli_connect_error());
}
mysqli_set_charset($linkz, "utf8");

// Get base URL - hardcoded for security
$base_url = 'https://comptantillimite.com';

// Start XML
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<!-- 
    SEO BY TIGUIDOU CREATION WEB | QUEBECSTORE.CA
    Client Website: https://comptantillimite.com
    Generated: <?php echo date('Y-m-d H:i:s'); ?>
-->
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
    
    <!-- Main Pages -->
    <url>
        <loc><?php echo $base_url; ?>/index.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>

    <url>
        <loc><?php echo $base_url; ?>/infolettre-comptant-illimite.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    
    <url>
        <loc><?php echo $base_url; ?>/produits.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>

    <url>
        <loc><?php echo $base_url; ?>/promotions.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    
    <url>
        <loc><?php echo $base_url; ?>/services.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    
    <url>
        <loc><?php echo $base_url; ?>/contact.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    
    <url>
        <loc><?php echo $base_url; ?>/bijouterie-sherbrooke.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>

    <url>
        <loc><?php echo $base_url; ?>/collectioneur.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    
    <url>
        <loc><?php echo $base_url; ?>/historique.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.5</priority>
    </url>
    
    <url>
        <loc><?php echo $base_url; ?>/confidentialite</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.3</priority>
    </url>
    
    <url>
        <loc><?php echo $base_url; ?>/temoins</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.3</priority>
    </url>
    
    <url>
        <loc><?php echo $base_url; ?>/gouvernance</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.3</priority>
    </url>
    
    <url>
        <loc><?php echo $base_url; ?>/conditions</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.3</priority>
    </url>
    
    <?php
    // Get all parent categories (main categories)
    $parent_query = "SELECT id, description, mise_a_jour FROM item_inventaire_webcategories2 WHERE parent = 0 AND count > 0 ORDER BY id";
    $parent_result = mysqli_query($linkz, $parent_query);
    
    if ($parent_result) {
        while ($parent_row = mysqli_fetch_array($parent_result, MYSQLI_ASSOC)) {
            $parent_id = intval($parent_row['id']);
            $lastmod = !empty($parent_row['mise_a_jour']) ? date('Y-m-d', strtotime($parent_row['mise_a_jour'])) : date('Y-m-d');
            ?>
    <url>
        <loc><?php echo htmlspecialchars($base_url . '/produits.php?parent=' . $parent_id, ENT_XML1, 'UTF-8'); ?></loc>
        <lastmod><?php echo htmlspecialchars($lastmod, ENT_XML1, 'UTF-8'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
            <?php
            
            // Get all subcategories for this parent
            $sub_query = sprintf("SELECT id, description, mise_a_jour FROM item_inventaire_webcategories2 WHERE parent = %d AND count > 0 ORDER BY id", $parent_id);
            $sub_result = mysqli_query($linkz, $sub_query);
            
            if ($sub_result) {
                while ($sub_row = mysqli_fetch_array($sub_result, MYSQLI_ASSOC)) {
                    $sub_id = intval($sub_row['id']);
                    $sub_lastmod = !empty($sub_row['mise_a_jour']) ? date('Y-m-d', strtotime($sub_row['mise_a_jour'])) : date('Y-m-d');
                    ?>
    <url>
        <loc><?php echo htmlspecialchars($base_url . '/produits.php?cat=' . $sub_id, ENT_XML1, 'UTF-8'); ?></loc>
        <lastmod><?php echo htmlspecialchars($sub_lastmod, ENT_XML1, 'UTF-8'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.7</priority>
    </url>
                    <?php
                }
            }
        }
    }
    
    // Get ALL products with afficher_web activated (no limit for comprehensive indexing)
    // Query includes all necessary fields for rich product schema
    // Updates every 24 hours via mise_a_jour timestamp
    $product_query = "SELECT 
                        item_inventaire.id, 
                        item_inventaire.name,
                        item_inventaire.prix_vente,
                        item_inventaire.rabais,
                        item_inventaire.web_description,
                        item_inventaire.marque,
                        item_inventaire.modele,
                        item_inventaire.mise_a_jour,
                        cat.description AS cat_descr,
                        parent_cat.description AS parent_cat_descr,
                        cat.id AS cat_id,
                        cat.parent AS parent_cat_id
                      FROM item_inventaire 
                      LEFT JOIN item_inventaire_webcategories2 AS cat 
                          ON (item_inventaire.id_item_inventaire_webcategories = cat.id)
                      LEFT JOIN item_inventaire_webcategories2 AS parent_cat
                          ON (cat.parent = parent_cat.id)
                      WHERE item_inventaire.afficher_web = '1' 
                      AND item_inventaire.id_status_inventaire IN (2,5,6)
                      ORDER BY item_inventaire.mise_a_jour DESC";
    $product_result = mysqli_query($linkz, $product_query);
    
    if ($product_result) {
        while ($product_row = mysqli_fetch_array($product_result, MYSQLI_ASSOC)) {
            $product_id = intval($product_row['id']);
            $product_name = $product_row['name'];
            $product_price = number_format((float)$product_row['prix_vente'], 2, '.', '');
            $product_rabais = isset($product_row['rabais']) ? floatval($product_row['rabais']) : 0;
            $product_lastmod = !empty($product_row['mise_a_jour']) ? date('Y-m-d\TH:i:s', strtotime($product_row['mise_a_jour'])) : date('Y-m-d\TH:i:s');
            
            // Category and subcategory
            $category = !empty($product_row['parent_cat_descr']) ? $product_row['parent_cat_descr'] : 'Général';
            $subcategory = !empty($product_row['cat_descr']) ? $product_row['cat_descr'] : '';
            
            // Brand and model
            $brand = !empty($product_row['marque']) ? $product_row['marque'] : '';
            $model = !empty($product_row['modele']) ? $product_row['modele'] : '';
            
            // Build rich description
            $description = '';
            if (!empty($product_row['web_description'])) {
                $description = strip_tags($product_row['web_description']);
            } else {
                $description_parts = array_filter([$brand, model, $category]);
                if (count($description_parts) > 0) {
                    $description = implode(' ', $description_parts) . '. Idéal pour ' . strtolower($category) . '. Qualité supérieure garantie.';
                } else {
                    $description = $product_name . '. Qualité supérieure garantie chez Comptant Illimité Sherbrooke.';
                }
            }
            
            $product_url = $base_url . '/details-produit.php?id=' . $product_id;
            $product_image = $base_url . '/images/image_item.php?id=' . $product_id . '&numero=1';
            
            // Create structured data for enhanced SEO (Google Product Schema)
            $product_schema = array(
                '@context' => 'https://schema.org/',
                '@type' => 'Product',
                'name' => $product_name,
                'sku' => (string)$product_id,
                'description' => substr($description, 0, 200),
                'image' => $product_image,
                'url' => $product_url,
                'brand' => array(
                    '@type' => 'Brand',
                    'name' => !empty($brand) ? $brand : 'Comptant Illimité'
                ),
                'category' => $category,
                'offers' => array(
                    '@type' => 'Offer',
                    'price' => $product_price,
                    'priceCurrency' => 'CAD',
                    'availability' => 'https://schema.org/InStock',
                    'seller' => array(
                        '@type' => 'Organization',
                        'name' => 'Comptant Illimité Sherbrooke'
                    )
                )
            );
            
            if (!empty($model)) {
                $product_schema['model'] = $model;
            }
            
            ?>
    <url>
        <loc><?php echo htmlspecialchars($product_url, ENT_XML1, 'UTF-8'); ?></loc>
        <lastmod><?php echo htmlspecialchars($product_lastmod, ENT_XML1, 'UTF-8'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.6</priority>
        <image:image>
            <image:loc><?php echo htmlspecialchars($product_image, ENT_XML1, 'UTF-8'); ?></image:loc>
            <image:title><?php echo htmlspecialchars($product_name, ENT_XML1, 'UTF-8'); ?></image:title>
            <image:caption><?php echo htmlspecialchars(substr($description, 0, 150), ENT_XML1, 'UTF-8'); ?></image:caption>
        </image:image>
    </url>
            <?php
        }
    }
    
    mysqli_close($linkz);
    ?>
</urlset>
