<?php
// Set header to XML
header('Content-Type: application/xml; charset=utf-8');

// Prevent direct access cache
header('Cache-Control: public, max-age=3600');

// Load configuration
require_once __DIR__ . '/../src/config/config.php';
require_once __DIR__ . '/../src/config/database.php';
require_once __DIR__ . '/../src/models/Product.php';
require_once __DIR__ . '/../src/helpers/SlugHelper.php';

// Start XML
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:hreflang="http://www.w3.org/1999/xhtml">' . "\n";

try {
    $product = new Product($pdo);
    
    // Get all products
    $allProducts = $product->getAll('RAND', 'ASC', 'es'); // Get all products
    
    // Home page URLs (both languages)
    echo "<url>\n";
    echo "  <loc>https://www.cocuan.com/</loc>\n";
    echo "  <lastmod>" . date('Y-m-d') . "</lastmod>\n";
    echo "  <changefreq>weekly</changefreq>\n";
    echo "  <priority>1.0</priority>\n";
    echo "  <hreflang:link rel=\"alternate\" hreflang=\"es\" href=\"https://www.cocuan.com/\" />\n";
    echo "  <hreflang:link rel=\"alternate\" hreflang=\"en\" href=\"https://www.cocuan.com/\" />\n";
    echo "</url>\n";
    
    // Legal pages
    echo "<url>\n";
    echo "  <loc>https://www.cocuan.com/terminos-legales.php</loc>\n";
    echo "  <lastmod>" . date('Y-m-d') . "</lastmod>\n";
    echo "  <changefreq>monthly</changefreq>\n";
    echo "  <priority>0.8</priority>\n";
    echo "</url>\n";
    
    // Product pages (both languages)
    if (!empty($allProducts)) {
        foreach ($allProducts as $prod) {
            // Generate slugs for both languages
            $slugEs = SlugHelper::generateSlug($prod['name_es']);
            $slugEn = SlugHelper::generateSlug($prod['name_en']);
            
            // Spanish version
            echo "<url>\n";
            echo "  <loc>https://www.cocuan.com/" . htmlspecialchars($slugEs) . "</loc>\n";
            echo "  <lastmod>" . (!empty($prod['updated_at']) ? $prod['updated_at'] : date('Y-m-d')) . "</lastmod>\n";
            echo "  <changefreq>weekly</changefreq>\n";
            echo "  <priority>0.9</priority>\n";
            echo "  <hreflang:link rel=\"alternate\" hreflang=\"es\" href=\"https://www.cocuan.com/" . htmlspecialchars($slugEs) . "\" />\n";
            echo "  <hreflang:link rel=\"alternate\" hreflang=\"en\" href=\"https://www.cocuan.com/" . htmlspecialchars($slugEn) . "\" />\n";
            echo "</url>\n";
            
            // English version (only if different from Spanish)
            if ($slugEn !== $slugEs) {
                echo "<url>\n";
                echo "  <loc>https://www.cocuan.com/" . htmlspecialchars($slugEn) . "</loc>\n";
                echo "  <lastmod>" . (!empty($prod['updated_at']) ? $prod['updated_at'] : date('Y-m-d')) . "</lastmod>\n";
                echo "  <changefreq>weekly</changefreq>\n";
                echo "  <priority>0.9</priority>\n";
                echo "  <hreflang:link rel=\"alternate\" hreflang=\"es\" href=\"https://www.cocuan.com/" . htmlspecialchars($slugEs) . "\" />\n";
                echo "  <hreflang:link rel=\"alternate\" hreflang=\"en\" href=\"https://www.cocuan.com/" . htmlspecialchars($slugEn) . "\" />\n";
                echo "</url>\n";
            }
        }
    }
    
} catch (Exception $e) {
    error_log("Error generating sitemap: " . $e->getMessage());
}

echo "</urlset>\n";
?>
