<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . '/src/Core/Config.php';
require_once __DIR__ . '/src/Core/Model.php';

use Mvr\Infralink\Core\Database;

$baseUrl = "https://infralinkchile.cl";

$db = Database::getConnection();

header("Content-Type: application/xml; charset=utf-8");

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

/* HOME */
echo "<url>";
echo "<loc>".htmlspecialchars($baseUrl)."</loc>";
echo "<changefreq>daily</changefreq>";
echo "<priority>1.0</priority>";
echo "</url>";

/* PRODUCTOS */
$stmt = $db->query("SELECT slug, updated_at FROM products WHERE active = 1");

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $slug = htmlspecialchars($row['slug']);
    echo "<url>";
    echo "<loc>$baseUrl/Productos/$slug</loc>";

    if(!empty($row['updated_at'])){
        $lastmod = date('Y-m-d', strtotime($row['updated_at']));
        echo "<lastmod>$lastmod</lastmod>";
    }

    echo "<changefreq>weekly</changefreq>";
    echo "<priority>0.8</priority>";
    echo "</url>";
}

echo "</urlset>";