﻿<?php
require_once "inc\config.php";

header("Content-Type: application/xml; charset=UTF-8");

$pdo = db();
$st = $pdo->query("SELECT id FROM products WHERE stock > 0 ORDER BY id DESC");
$products = $st->fetchAll();

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";

// URLs principales
$urls = [
    ['loc' => 'https://obturia.es/', 'freq' => 'daily', 'priority' => '1.0'],
    ['loc' => 'https://obturia.es/catalogo.php', 'freq' => 'daily', 'priority' => '0.9'],
    ['loc' => 'https://obturia.es/legal.php?p=aviso', 'freq' => 'yearly', 'priority' => '0.5'],
    ['loc' => 'https://obturia.es/legal.php?p=privacidad', 'freq' => 'yearly', 'priority' => '0.5'],
    ['loc' => 'https://obturia.es/legal.php?p=envios', 'freq' => 'yearly', 'priority' => '0.5'],
    ['loc' => 'https://obturia.es/legal.php?p=devoluciones', 'freq' => 'yearly', 'priority' => '0.5'],
    ['loc' => 'https://obturia.es/legal.php?p=condiciones', 'freq' => 'yearly', 'priority' => '0.5'],
    ['loc' => 'https://obturia.es/contacto.php', 'freq' => 'monthly', 'priority' => '0.5'],
];

foreach ($urls as $url) {
    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($url['loc']) . "</loc>\n";
    echo "    <lastmod>" . date('Y-m-d') . "</lastmod>\n";
    echo "    <changefreq>" . $url['freq'] . "</changefreq>\n";
    echo "    <priority>" . $url['priority'] . "</priority>\n";
    echo "  </url>\n";
}

// URLs de productos
foreach ($products as $p) {
    echo "  <url>\n";
    echo "    <loc>https://obturia.es/producto.php?id=" . $p['id'] . "</loc>\n";
    echo "    <lastmod>" . date('Y-m-d') . "</lastmod>\n";
    echo "    <changefreq>weekly</changefreq>\n";
    echo "    <priority>0.7</priority>\n";
    echo "  </url>\n";
}

echo "</urlset>\n";
?>
