<?php

// Conectar a la base de datos
function getDBConnection() {
    $db_host = 'localhost';
    $db_user = 'plusmusi_owner';
    $db_pass = '^@#F-NBx3W}+';
    $db_name = 'plusmusi_data';

    try {
        $conectar = new mysqli($db_host, $db_user, $db_pass, $db_name);
        if ($conectar->connect_error) {
            throw new Exception("Error de conexión: " . $conectar->connect_error);
        }
        $conectar->set_charset("utf8mb4");
    } catch (Exception $e) {
        echo "Error: " . $e->getMessage();
        exit();
    }

    return $conectar;
}

$conectar = getDBConnection();

// Consultar la tabla biografias
$query = "SELECT link_bioartista, fecha_publicacion FROM biografias";
$resultado = $conectar->query($query);

// Comenzar el XML
header("Content-type: text/xml");

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

// Generar las entradas del sitemap
while ($fila = $resultado->fetch_assoc()) {
    $url = "https://wvw.plusmusicas.com/biografia/" . htmlspecialchars($fila['link_bioartista'], ENT_QUOTES, 'UTF-8');
    $fecha = date('Y-m-d', strtotime($fila['fecha_publicacion']));
    
    echo "  <url>\n";
    echo "    <loc>$url</loc>\n";
    echo "    <lastmod>$fecha</lastmod>\n";
    echo "    <changefreq>weekly</changefreq>\n";
    echo "    <priority>0.8</priority>\n";
    echo "  </url>\n";
}

echo "</urlset>\n";

$conectar->close();
