using FluentAssertions;
using SatelliteProvider.Common.Utils;

namespace SatelliteProvider.Tests;

// AZ-503 AC-1: Uuidv5.Create must produce byte-identical output to Python's
// stdlib `uuid.uuid5(namespace, name)`. Expected values below were generated
// against TILE_NAMESPACE = 5b8d0c2e-7f1a-4d3b-9c5e-1f3a8e7d2b6c using Python
// 3.x's `uuid` module and pasted as fixed-string assertions (per AZ-503 spec
// Risk 2 mitigation — vectors are explicit, not computed at test time).
//
// The cross-repo contract: gps-denied-onboard `c6_tile_cache/_uuid.py` MUST
// use the SAME namespace constant and Python's stdlib `uuid.uuid5`. Both sides
// therefore compute identical IDs for identical (namespace, name) inputs.
public class Uuidv5Tests
{
    [Theory]
    [InlineData("18/12345/23456/google_maps/00000000-0000-0000-0000-000000000000", "89e9514c-066d-5015-973f-ac42758ebf37")]
    [InlineData("18/12345/23456", "38b26f49-a966-5121-aaf4-9cc476f57869")]
    [InlineData("15/0/0/google_maps/00000000-0000-0000-0000-000000000000", "82a17784-50f3-58e2-b3a1-5da8224ff19d")]
    [InlineData("20/1048575/1048575/uav/11111111-2222-3333-4444-555555555555", "9aaefb75-68c1-5691-89b4-3552323ef5de")]
    [InlineData("16/76800/50331/google_maps/00000000-0000-0000-0000-000000000000", "88576e42-70ae-5977-a809-014d1448b012")]
    [InlineData("18/12345/23456/uav/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", "7d3c86e8-ce9b-5e40-9a08-8ffe6bab346a")]
    [InlineData("0/0/0/google_maps/00000000-0000-0000-0000-000000000000", "d6557888-270b-59a9-9f21-652fdd0a9e50")]
    [InlineData("simple-ascii-name", "d33497cd-8017-5ed0-9f9a-b6ff3c852b2a")]
    [InlineData("unicode-naïveté-✓", "06710360-b8f0-5fe0-8f0a-5e934216e536")]
    [InlineData("18/76800/50331", "5993e42c-a647-5802-b3a4-50105365832c")]
    [InlineData("17/57842/41320/uav/12345678-1234-1234-1234-123456789012", "c5a0cac0-7155-5e49-91b8-f29bb0342a96")]
    public void Create_MatchesPythonUuid5_ForReferenceVectors(string name, string expectedUuid)
    {
        // Act
        var result = Uuidv5.Create(Uuidv5.TileNamespace, name);

        // Assert
        result.Should().Be(Guid.Parse(expectedUuid),
            $"C# Uuidv5.Create must match Python uuid.uuid5({Uuidv5.TileNamespace}, \"{name}\") = {expectedUuid}");
    }

    [Fact]
    public void Create_IsDeterministic()
    {
        // Arrange
        const string name = "18/12345/23456/google_maps/00000000-0000-0000-0000-000000000000";

        // Act
        var first = Uuidv5.Create(Uuidv5.TileNamespace, name);
        var second = Uuidv5.Create(Uuidv5.TileNamespace, name);

        // Assert
        first.Should().Be(second, "deterministic algorithm must produce identical output for identical inputs");
    }

    [Fact]
    public void Create_ProducesVersion5AndRfc4122Variant()
    {
        // Arrange
        var name = "any-name";

        // Act
        var uuid = Uuidv5.Create(Uuidv5.TileNamespace, name);
        var bytes = uuid.ToByteArray();
        // Guid.ToByteArray returns mixed-endian; for version/variant bits we
        // need big-endian byte 6 (version) and byte 8 (variant). Reconstruct
        // the big-endian view: bytes 0..3 reversed, 4..5 reversed, 6..7
        // reversed, 8..15 as-is.
        var bigEndian = new byte[16];
        bigEndian[0] = bytes[3]; bigEndian[1] = bytes[2]; bigEndian[2] = bytes[1]; bigEndian[3] = bytes[0];
        bigEndian[4] = bytes[5]; bigEndian[5] = bytes[4];
        bigEndian[6] = bytes[7]; bigEndian[7] = bytes[6];
        Array.Copy(bytes, 8, bigEndian, 8, 8);

        // Assert
        ((bigEndian[6] & 0xF0) >> 4).Should().Be(5, "version nibble (upper 4 bits of byte 6) must be 5 per RFC 9562 §5.5");
        (bigEndian[8] & 0xC0).Should().Be(0x80, "variant bits (upper 2 of byte 8) must be 10 per RFC 4122 variant");
    }

    [Fact]
    public void Create_DifferentNamesProduceDifferentUuids()
    {
        // Arrange
        var name1 = "18/12345/23456/google_maps/00000000-0000-0000-0000-000000000000";
        var name2 = "18/12345/23456/google_maps/11111111-1111-1111-1111-111111111111";

        // Act
        var uuid1 = Uuidv5.Create(Uuidv5.TileNamespace, name1);
        var uuid2 = Uuidv5.Create(Uuidv5.TileNamespace, name2);

        // Assert
        uuid1.Should().NotBe(uuid2, "different flight_id values must produce different tile ids");
    }

    [Fact]
    public void Create_ThrowsOnNullName()
    {
        // Act
        var act = () => Uuidv5.Create(Uuidv5.TileNamespace, null!);

        // Assert
        act.Should().Throw<ArgumentNullException>();
    }
}
