using System.Text; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using SatelliteProvider.Common.Configs; using SatelliteProvider.DataAccess.Models; namespace SatelliteProvider.Services.RouteManagement; // AZ-364 / C11: extracted from RouteProcessingService.GenerateRouteSummaryAsync. // Owns the route__summary.txt path and the StringBuilder block that // describes the route, tile statistics, and produced files. Output content // preserved verbatim to satisfy AC-2 (byte-identical for existing scenarios). public class RouteSummaryWriter { private readonly StorageConfig _storageConfig; private readonly ILogger _logger; public RouteSummaryWriter(IOptions storageConfig, ILogger logger) { ArgumentNullException.ThrowIfNull(storageConfig); _storageConfig = storageConfig.Value; _logger = logger; } public async Task WriteAsync( RouteEntity route, int uniqueTiles, int totalTilesFromRegions, int duplicateTiles, string? tilesZipPath, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(route); Directory.CreateDirectory(_storageConfig.ReadyDirectory); var filePath = Path.Combine(_storageConfig.ReadyDirectory, $"route_{route.Id}_summary.txt"); var summary = new StringBuilder(); summary.AppendLine("Route Maps Summary"); summary.AppendLine("=================="); summary.AppendLine($"Route ID: {route.Id}"); summary.AppendLine($"Route Name: {route.Name}"); if (!string.IsNullOrEmpty(route.Description)) { summary.AppendLine($"Description: {route.Description}"); } summary.AppendLine($"Total Points: {route.TotalPoints}"); summary.AppendLine($"Total Distance: {route.TotalDistanceMeters:F2} meters"); summary.AppendLine($"Region Size: {route.RegionSizeMeters:F0} meters"); summary.AppendLine($"Zoom Level: {route.ZoomLevel}"); summary.AppendLine(); summary.AppendLine("Tile Statistics:"); summary.AppendLine($"- Unique Tiles: {uniqueTiles}"); summary.AppendLine($"- Total Tiles from Regions: {totalTilesFromRegions}"); summary.AppendLine($"- Duplicate Tiles (overlap): {duplicateTiles}"); summary.AppendLine(); summary.AppendLine("Files Created:"); summary.AppendLine($"- CSV: route_{route.Id}_ready.csv"); summary.AppendLine($"- Summary: route_{route.Id}_summary.txt"); if (route.RequestMaps) { summary.AppendLine($"- Stitched Map: route_{route.Id}_stitched.jpg"); } if (tilesZipPath != null) { summary.AppendLine($"- Tiles ZIP: route_{route.Id}_tiles.zip"); } summary.AppendLine(); summary.AppendLine($"Completed: {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC"); await File.WriteAllTextAsync(filePath, summary.ToString(), cancellationToken); _logger.LogInformation("Route summary generated: {FilePath}", filePath); return filePath; } }