using System.Text;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using MotoresArgentinosV2.Core.Entities;
using MotoresArgentinosV2.Infrastructure.Data;
namespace MotoresArgentinosV2.Infrastructure.Services;
///
/// Servicio de fondo que genera sitemap.xml dinámicamente.
/// Ejecuta al iniciar la app y luego cada 6 horas.
/// Incluye rutas estáticas del frontend + vehículos activos.
///
public class SitemapGeneratorService : BackgroundService
{
private readonly IServiceProvider _serviceProvider;
private readonly ILogger _logger;
private static readonly TimeSpan Interval = TimeSpan.FromHours(6);
private const string BaseUrl = "https://motoresargentinos.com";
// Rutas estáticas cuyo contenido cambia con cada aviso nuevo (usan lastmod dinámico)
private static readonly (string Path, string Priority, string ChangeFreq)[] DynamicDateRoutes =
[
("/", "1.0", "daily"),
("/explorar", "0.8", "daily"),
];
// Rutas estáticas cuyo contenido rara vez cambia (sin lastmod para no mentirle a Google)
private static readonly (string Path, string Priority, string ChangeFreq)[] FixedRoutes =
[
("/publicar", "0.6", "monthly"),
("/vender", "0.6", "monthly"),
("/condiciones", "0.3", "yearly"),
];
public SitemapGeneratorService(IServiceProvider serviceProvider, ILogger logger)
{
_serviceProvider = serviceProvider;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("SitemapGeneratorService iniciado.");
while (!stoppingToken.IsCancellationRequested)
{
try
{
await GenerateSitemapAsync();
}
catch (OperationCanceledException)
{
break;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error generando sitemap.");
}
await Task.Delay(Interval, stoppingToken);
}
}
private async Task GenerateSitemapAsync()
{
using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService();
var config = scope.ServiceProvider.GetRequiredService();
// Ruta de salida: configurable para Docker (volumen compartido) o dev local
var outputPath = config["AppSettings:SitemapOutputPath"] ?? Path.Combine("wwwroot", "sitemap.xml");
// Obtener todos los avisos activos
var activeAds = await context.Ads
.AsNoTracking()
.Where(a => a.StatusID == (int)AdStatusEnum.Active)
.Select(a => new { a.AdID, a.PublishedAt })
.ToListAsync();
// Fecha del aviso más reciente (para rutas cuyo contenido cambia con nuevos avisos)
var latestPublished = activeAds
.Where(a => a.PublishedAt.HasValue)
.Max(a => a.PublishedAt)
?.ToString("yyyy-MM-dd") ?? DateTime.UtcNow.ToString("yyyy-MM-dd");
var sb = new StringBuilder();
sb.AppendLine("");
sb.AppendLine("");
// 1. Rutas cuyo contenido cambia con cada aviso nuevo (/, /explorar)
foreach (var (path, priority, changeFreq) in DynamicDateRoutes)
{
sb.AppendLine(" ");
sb.AppendLine($" {BaseUrl}{path}");
sb.AppendLine($" {latestPublished}");
sb.AppendLine($" {changeFreq}");
sb.AppendLine($" {priority}");
sb.AppendLine(" ");
}
// 2. Rutas fijas (sin lastmod — no mentirle a Google)
foreach (var (path, priority, changeFreq) in FixedRoutes)
{
sb.AppendLine(" ");
sb.AppendLine($" {BaseUrl}{path}");
sb.AppendLine($" {changeFreq}");
sb.AppendLine($" {priority}");
sb.AppendLine(" ");
}
// 3. Rutas dinámicas (vehículos activos — lastmod = fecha de publicación real)
foreach (var ad in activeAds)
{
var lastmod = ad.PublishedAt?.ToString("yyyy-MM-dd") ?? latestPublished;
sb.AppendLine(" ");
sb.AppendLine($" {BaseUrl}/vehiculo/{ad.AdID}");
sb.AppendLine($" {lastmod}");
sb.AppendLine(" weekly");
sb.AppendLine(" 0.7");
sb.AppendLine(" ");
}
sb.AppendLine("");
// Escritura atómica: escribir en .tmp, luego mover
var dir = Path.GetDirectoryName(outputPath);
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
var tempPath = outputPath + ".tmp";
await File.WriteAllTextAsync(tempPath, sb.ToString(), Encoding.UTF8);
File.Move(tempPath, outputPath, overwrite: true);
var totalUrls = DynamicDateRoutes.Length + FixedRoutes.Length + activeAds.Count;
_logger.LogInformation("Sitemap generado con {TotalUrls} URLs ({StaticCount} estáticas + {DynamicCount} vehículos). Archivo: {Path}",
totalUrls, DynamicDateRoutes.Length + FixedRoutes.Length, activeAds.Count, outputPath);
}
}