using System.Text.Json;
using FluentAssertions;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Moq;
using SatelliteProvider.Common.Configs;
using SatelliteProvider.Common.DTO;
using SatelliteProvider.Common.Enums;
using SatelliteProvider.DataAccess.Models;
using SatelliteProvider.DataAccess.Repositories;
using SatelliteProvider.Services.TileDownloader;
using SatelliteProvider.Tests.TestUtilities;

namespace SatelliteProvider.Tests;

public class UavTileUploadHandlerTests : IDisposable
{
    private readonly string _tilesRoot;

    public UavTileUploadHandlerTests()
    {
        _tilesRoot = Path.Combine(Path.GetTempPath(), "satprov-uavtests-" + Guid.NewGuid().ToString("N"));
        Directory.CreateDirectory(_tilesRoot);
    }

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

    [Fact]
    public async Task HandleAsync_HappyPath_PersistsRowAndAcceptsItem()
    {
        // Arrange
        var jpeg = UavTileImageFactory.CreateRandomJpeg();
        var metadata = ValidMetadata();
        var (handler, repo) = BuildHandler();
        var inserted = new List<TileEntity>();
        repo.Setup(r => r.InsertAsync(It.IsAny<TileEntity>()))
            .ReturnsAsync(Guid.NewGuid())
            .Callback<TileEntity>(e => inserted.Add(e));

        // Act
        var result = await handler.HandleAsync(
            JsonSerializer.Serialize(new UavTileBatchMetadataPayload { Items = { metadata } }),
            new List<UavUploadFile> { new("tile.jpg", "image/jpeg", jpeg) });

        // Assert
        result.EnvelopeRejected.Should().BeFalse();
        result.Response!.Items.Should().HaveCount(1);
        result.Response.Items[0].Status.Should().Be(UavTileUploadStatus.Accepted);
        result.Response.Items[0].TileId.Should().NotBeNull();
        inserted.Should().HaveCount(1);
        inserted[0].Source.Should().Be(TileSourceConverter.ToWireValue(TileSource.Uav));
        // AZ-503: flight-anonymous upload (ValidMetadata has FlightId=null) uses the
        // literal "none" segment between "uav" and the zoom directory.
        inserted[0].FilePath.Should().Contain(Path.Combine("uav", "none", "18"));
        inserted[0].LocationHash.Should().NotBe(Guid.Empty,
            "AZ-503: location_hash must be deterministic UUIDv5(TILE_NAMESPACE, \"z/x/y\")");
        inserted[0].ContentSha256.Should().NotBeNullOrEmpty(
            "AZ-503 AC-7: content_sha256 must be persisted for every UAV upload");
        File.Exists(inserted[0].FilePath).Should().BeTrue();
    }

    [Fact]
    public async Task HandleAsync_MixedBatch_AcceptsValidRejectsBadItems()
    {
        // Arrange
        var goodJpeg = UavTileImageFactory.CreateRandomJpeg();
        var wrongDimensions = UavTileImageFactory.CreateRandomJpeg(width: 512, height: 512);
        var notJpeg = new byte[6000];
        notJpeg[0] = 0x89; notJpeg[1] = 0x50; notJpeg[2] = 0x4E; notJpeg[3] = 0x47;
        var meta = ValidMetadata();
        var (handler, repo) = BuildHandler();
        repo.Setup(r => r.InsertAsync(It.IsAny<TileEntity>())).ReturnsAsync(Guid.NewGuid());

        // Act
        var result = await handler.HandleAsync(
            JsonSerializer.Serialize(new UavTileBatchMetadataPayload
            {
                Items = { meta, meta with { Latitude = meta.Latitude + 0.001 }, meta with { Latitude = meta.Latitude + 0.002 } }
            }),
            new List<UavUploadFile>
            {
                new("a.jpg", "image/jpeg", goodJpeg),
                new("b.jpg", "image/jpeg", wrongDimensions),
                new("c.jpg", "image/jpeg", notJpeg),
            });

        // Assert
        result.EnvelopeRejected.Should().BeFalse();
        result.Response!.Items.Should().HaveCount(3);
        result.Response.Items[0].Status.Should().Be(UavTileUploadStatus.Accepted);
        result.Response.Items[1].Status.Should().Be(UavTileUploadStatus.Rejected);
        result.Response.Items[1].RejectReason.Should().Be(UavTileRejectReasons.WrongDimensions);
        result.Response.Items[2].Status.Should().Be(UavTileUploadStatus.Rejected);
        result.Response.Items[2].RejectReason.Should().Be(UavTileRejectReasons.InvalidFormat);
        repo.Verify(r => r.InsertAsync(It.IsAny<TileEntity>()), Times.Once,
            "only the single accepted item should reach the repository");
    }

    [Fact]
    public async Task HandleAsync_OversizedBatch_ReturnsEnvelopeError()
    {
        // Arrange
        var (handler, _) = BuildHandler(new UavQualityConfig { MaxBatchSize = 2 });
        var metadata = ValidMetadata();
        var payload = new UavTileBatchMetadataPayload { Items = { metadata, metadata, metadata } };
        var jpeg = UavTileImageFactory.CreateRandomJpeg();

        // Act
        var result = await handler.HandleAsync(
            JsonSerializer.Serialize(payload),
            new List<UavUploadFile>
            {
                new("a.jpg", "image/jpeg", jpeg),
                new("b.jpg", "image/jpeg", jpeg),
                new("c.jpg", "image/jpeg", jpeg),
            });

        // Assert
        result.EnvelopeRejected.Should().BeTrue();
        result.EnvelopeError.Should().Contain("exceeds the configured maximum");
        result.Response.Should().BeNull();
    }

    [Fact]
    public async Task HandleAsync_MismatchedFilesAndMetadata_ReturnsEnvelopeError()
    {
        // Arrange
        var (handler, _) = BuildHandler();
        var jpeg = UavTileImageFactory.CreateRandomJpeg();

        // Act
        var result = await handler.HandleAsync(
            JsonSerializer.Serialize(new UavTileBatchMetadataPayload { Items = { ValidMetadata(), ValidMetadata() } }),
            new List<UavUploadFile> { new("a.jpg", "image/jpeg", jpeg) });

        // Assert
        result.EnvelopeRejected.Should().BeTrue();
        result.EnvelopeError.Should().Contain("Mismatched batch");
    }

    [Fact]
    public async Task HandleAsync_EmptyMetadata_ReturnsEnvelopeError()
    {
        // Arrange
        var (handler, _) = BuildHandler();

        // Act
        var result = await handler.HandleAsync(
            metadataJson: "",
            files: new List<UavUploadFile>());

        // Assert
        result.EnvelopeRejected.Should().BeTrue();
    }

    [Fact]
    public async Task HandleAsync_InvalidMetadataJson_ReturnsEnvelopeError()
    {
        // Arrange
        var (handler, _) = BuildHandler();

        // Act
        var result = await handler.HandleAsync(
            metadataJson: "{ not valid json",
            files: new List<UavUploadFile>());

        // Assert
        result.EnvelopeRejected.Should().BeTrue();
        result.EnvelopeError.Should().Be("`metadata` could not be parsed as JSON.");
    }

    [Fact]
    public async Task HandleAsync_TwoFlightsSameCell_ProduceDistinctIdsAndPathsButSameLocationHash()
    {
        // Arrange — AZ-503 AC-3 + AC-11: two flights uploading the same (z, x, y).
        var jpegA = UavTileImageFactory.CreateRandomJpeg();
        var jpegB = UavTileImageFactory.CreateRandomJpeg();
        var f1 = Guid.Parse("11111111-1111-1111-1111-111111111111");
        var f2 = Guid.Parse("22222222-2222-2222-2222-222222222222");
        var metaA = ValidMetadata() with { FlightId = f1 };
        var metaB = ValidMetadata() with { FlightId = f2 };
        var (handler, repo) = BuildHandler();
        var inserted = new List<TileEntity>();
        repo.Setup(r => r.InsertAsync(It.IsAny<TileEntity>()))
            .ReturnsAsync((TileEntity e) => e.Id)
            .Callback<TileEntity>(e => inserted.Add(e));

        // Act
        await handler.HandleAsync(
            JsonSerializer.Serialize(new UavTileBatchMetadataPayload { Items = { metaA } }),
            new List<UavUploadFile> { new("a.jpg", "image/jpeg", jpegA) });
        await handler.HandleAsync(
            JsonSerializer.Serialize(new UavTileBatchMetadataPayload { Items = { metaB } }),
            new List<UavUploadFile> { new("b.jpg", "image/jpeg", jpegB) });

        // Assert
        inserted.Should().HaveCount(2);
        inserted[0].FlightId.Should().Be(f1);
        inserted[1].FlightId.Should().Be(f2);
        inserted[0].Id.Should().NotBe(inserted[1].Id, "AC-3: per-flight rows must have distinct deterministic ids");
        inserted[0].LocationHash.Should().Be(inserted[1].LocationHash,
            "AC-3: both rows share the same location_hash because (z, x, y) is identical");
        inserted[0].FilePath.Should().NotBe(inserted[1].FilePath,
            "AC-11: per-flight on-disk paths must differ");
        inserted[0].FilePath.Should().Contain(f1.ToString());
        inserted[1].FilePath.Should().Contain(f2.ToString());
        File.Exists(inserted[0].FilePath).Should().BeTrue();
        File.Exists(inserted[1].FilePath).Should().BeTrue();
    }

    [Fact]
    public async Task HandleAsync_IdenticalUpload_ProducesIdenticalIdAndDeterministicContentSha()
    {
        // Arrange — AZ-503 AC-2 + AC-7: same inputs → same UUIDv5 + same SHA-256.
        var jpeg = UavTileImageFactory.CreateRandomJpeg();
        var meta = ValidMetadata() with { FlightId = Guid.Parse("33333333-3333-3333-3333-333333333333") };
        var (handler, repo) = BuildHandler();
        var inserted = new List<TileEntity>();
        repo.Setup(r => r.InsertAsync(It.IsAny<TileEntity>()))
            .ReturnsAsync((TileEntity e) => e.Id)
            .Callback<TileEntity>(e => inserted.Add(e));

        // Act
        await handler.HandleAsync(
            JsonSerializer.Serialize(new UavTileBatchMetadataPayload { Items = { meta } }),
            new List<UavUploadFile> { new("first.jpg", "image/jpeg", jpeg) });
        await handler.HandleAsync(
            JsonSerializer.Serialize(new UavTileBatchMetadataPayload { Items = { meta } }),
            new List<UavUploadFile> { new("second.jpg", "image/jpeg", jpeg) });

        // Assert
        inserted.Should().HaveCount(2);
        inserted[0].Id.Should().Be(inserted[1].Id, "AC-2: identical inputs must produce identical deterministic ids");
        inserted[0].LocationHash.Should().Be(inserted[1].LocationHash);
        inserted[0].ContentSha256.Should().BeEquivalentTo(inserted[1].ContentSha256,
            "AC-7: identical JPEG bodies must produce identical SHA-256 digests");
        inserted[0].ContentSha256!.Length.Should().Be(32, "SHA-256 always produces 32 bytes");
    }

    private (UavTileUploadHandler Handler, Mock<ITileRepository> Repo) BuildHandler(UavQualityConfig? quality = null)
    {
        var qualityConfig = quality ?? new UavQualityConfig();
        var mapConfig = new MapConfig { TileSizePixels = 256 };
        var storageConfig = new StorageConfig { TilesDirectory = _tilesRoot };
        var time = new FixedTimeProvider(new DateTime(2026, 5, 11, 12, 0, 0, DateTimeKind.Utc));

        var gate = new UavTileQualityGate(
            Options.Create(qualityConfig),
            Options.Create(mapConfig),
            time);

        var repo = new Mock<ITileRepository>();
        var handler = new UavTileUploadHandler(
            gate,
            repo.Object,
            Options.Create(storageConfig),
            Options.Create(mapConfig),
            Options.Create(qualityConfig),
            NullLogger<UavTileUploadHandler>.Instance,
            time);

        return (handler, repo);
    }

    private static UavTileMetadata ValidMetadata() => new()
    {
        Latitude = 47.461747,
        Longitude = 37.647063,
        TileZoom = 18,
        TileSizeMeters = 200.0,
        CapturedAt = new DateTime(2026, 5, 11, 11, 30, 0, DateTimeKind.Utc),
    };

    private sealed class FixedTimeProvider : TimeProvider
    {
        private readonly DateTime _utcNow;
        public FixedTimeProvider(DateTime utcNow) => _utcNow = utcNow;
        public override DateTimeOffset GetUtcNow() => new(_utcNow, TimeSpan.Zero);
    }
}
