using System.Security.Cryptography;
using FluentAssertions;
using SatelliteProvider.Services.RouteManagement.TileProvision;

namespace SatelliteProvider.Tests;

public class ClientTileCatalogTests
{
    private static readonly byte[] ServerHash = SHA256.HashData("server-tile"u8.ToArray());
    private static readonly byte[] ClientHash = SHA256.HashData("client-tile"u8.ToArray());

    [Fact]
    public void ShouldSkipForClient_HashMatch_Skips()
    {
        var client = new ClientTileSnapshot(18, 1, 2, 1.0, DateTime.UtcNow.AddDays(-1), ServerHash);
        var server = new ServerTileProspect(2.0, DateTime.UtcNow, ServerHash);

        ClientTileCatalog.ShouldSkipForClient(client, server).Should().BeTrue();
    }

    [Fact]
    public void ShouldSkipForClient_MetadataSufficient_Skips()
    {
        var client = new ClientTileSnapshot(18, 1, 2, 0.5, DateTime.UtcNow, null);
        var server = new ServerTileProspect(1.0, DateTime.UtcNow.AddHours(-1), null);

        ClientTileCatalog.ShouldSkipForClient(client, server).Should().BeTrue();
    }

    [Fact]
    public void ShouldSkipForClient_WorseResolution_DoesNotSkip()
    {
        var client = new ClientTileSnapshot(18, 1, 2, 2.0, DateTime.UtcNow, null);
        var server = new ServerTileProspect(1.0, DateTime.UtcNow, null);

        ClientTileCatalog.ShouldSkipForClient(client, server).Should().BeFalse();
    }

    [Fact]
    public void ShouldSkipForClient_OlderCapture_DoesNotSkip()
    {
        var client = new ClientTileSnapshot(18, 1, 2, 0.5, DateTime.UtcNow.AddDays(-2), null);
        var server = new ServerTileProspect(1.0, DateTime.UtcNow, null);

        ClientTileCatalog.ShouldSkipForClient(client, server).Should().BeFalse();
    }

    [Fact]
    public void ShouldSkipForClient_NoClientRecord_DoesNotSkip()
    {
        var server = new ServerTileProspect(1.0, DateTime.UtcNow, ServerHash);

        ClientTileCatalog.ShouldSkipForClient(null, server).Should().BeFalse();
    }

    [Fact]
    public void ShouldSkipForClient_DifferentHashWithInsufficientMetadata_DoesNotSkip()
    {
        var client = new ClientTileSnapshot(18, 1, 2, 2.0, DateTime.UtcNow.AddDays(-2), ClientHash);
        var server = new ServerTileProspect(1.0, DateTime.UtcNow, ServerHash);

        ClientTileCatalog.ShouldSkipForClient(client, server).Should().BeFalse();
    }
}
