using FluentAssertions;
using SatelliteProvider.Common.Configs;

namespace SatelliteProvider.Tests;

public class StorageConfigTests
{
    [Fact]
    public void TryExtractTileCoordinates_ValidFilename_ReturnsTrue_AZ366()
    {
        var ok = StorageConfig.TryExtractTileCoordinates(
            "/tiles/18/158/90/tile_18_158292_90821_20260510022758.jpg",
            out var x,
            out var y);

        ok.Should().BeTrue();
        x.Should().Be(158292);
        y.Should().Be(90821);
    }

    [Fact]
    public void TryExtractTileCoordinates_FilenameWithoutDirectory_ReturnsTrue_AZ366()
    {
        var ok = StorageConfig.TryExtractTileCoordinates(
            "tile_1700000000_42_99.jpg",
            out var x,
            out var y);

        ok.Should().BeTrue();
        x.Should().Be(42);
        y.Should().Be(99);
    }

    [Fact]
    public void TryExtractTileCoordinates_NonTilePrefix_ReturnsFalseAndSentinel_AZ366()
    {
        var ok = StorageConfig.TryExtractTileCoordinates(
            "/tmp/not_a_tile_filename.jpg",
            out var x,
            out var y);

        ok.Should().BeFalse();
        x.Should().Be(-1);
        y.Should().Be(-1);
    }

    [Fact]
    public void TryExtractTileCoordinates_NonNumericCoords_ReturnsFalseAndSentinel_AZ366()
    {
        var ok = StorageConfig.TryExtractTileCoordinates(
            "/tiles/tile_1700000000_alpha_beta.jpg",
            out var x,
            out var y);

        ok.Should().BeFalse();
        x.Should().Be(-1);
        y.Should().Be(-1);
    }

    [Fact]
    public void TryExtractTileCoordinates_NullPath_ThrowsArgumentNullException_AZ366()
    {
        Action act = () => StorageConfig.TryExtractTileCoordinates(null!, out _, out _);

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

    [Fact]
    public void GetTileFilePath_RoundTrip_ParserRecoversOriginalCoordinates_AZ366_AC2()
    {
        // Arrange
        var config = new StorageConfig();
        const int zoom = 18;
        const int writtenX = 158_292;
        const int writtenY = 90_821;

        // Act
        var path = config.GetTileFilePath(zoom, writtenX, writtenY, "20260510022758");
        var ok = StorageConfig.TryExtractTileCoordinates(path, out var parsedX, out var parsedY);

        // Assert
        ok.Should().BeTrue();
        parsedX.Should().Be(writtenX, "writer-parser pair must be inverse — same module per AZ-366 AC-2");
        parsedY.Should().Be(writtenY);
    }
}
