using FluentAssertions;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using SatelliteProvider.Common.Configs;
using SatelliteProvider.Services.RouteManagement;

namespace SatelliteProvider.Tests;

// AZ-364 / C11: route-side CSV writer wraps the shared TileCsvWriter and
// owns the route_<id>_ready.csv path. Only the wrapping behavior (path,
// row mapping, returned path) is tested here; CSV byte format is the
// shared TileCsvWriter's concern (covered separately).
public class RouteCsvWriterTests : IDisposable
{
    private readonly string _tempDir;

    public RouteCsvWriterTests()
    {
        _tempDir = Path.Combine(Path.GetTempPath(), "az364_routecsv_" + Guid.NewGuid().ToString("N"));
        Directory.CreateDirectory(_tempDir);
    }

    public void Dispose()
    {
        if (Directory.Exists(_tempDir))
        {
            Directory.Delete(_tempDir, recursive: true);
        }
        GC.SuppressFinalize(this);
    }

    [Fact]
    public async Task WriteAsync_ProducesExpectedFileAndReturnsItsPath_AZ364_AC1()
    {
        // Arrange
        var routeId = Guid.NewGuid();
        var storageOptions = Options.Create(new StorageConfig { ReadyDirectory = _tempDir });
        var sut = new RouteCsvWriter(storageOptions, NullLogger<RouteCsvWriter>.Instance);
        var tiles = new List<TileInfo>
        {
            new(40.0, -73.0, "/tiles/a.jpg"),
            new(41.0, -74.0, "/tiles/b.jpg"),
        };

        // Act
        var path = await sut.WriteAsync(routeId, tiles);

        // Assert
        path.Should().Be(Path.Combine(_tempDir, $"route_{routeId}_ready.csv"));
        File.Exists(path).Should().BeTrue();

        var lines = await File.ReadAllLinesAsync(path);
        lines.Should().HaveCount(3);
        lines[0].Should().Be("latitude,longitude,file_path");
        lines.Should().Contain("41.000000,-74.000000,/tiles/b.jpg");
        lines.Should().Contain("40.000000,-73.000000,/tiles/a.jpg");
    }
}
