using Theriapolis.Core.World.Generation;
using Xunit;

namespace Theriapolis.Tests.Determinism;

/// <summary>
/// Phase 1 determinism contract:
/// seed 0xCAFEBABE run twice → byte-identical elevation, moisture, temperature,
/// and biome arrays.
///
/// Uses variant 0 and variant 1 so the WorldCache fixture returns two
/// independent pipeline runs of the same seed (otherwise comparing the cached
/// context against itself would prove nothing).
/// </summary>
public sealed class WorldgenDeterminismTests : IClassFixture<WorldCache>
{
    private const ulong TestSeed = 0xCAFEBABEUL;
    private readonly WorldCache _cache;

    public WorldgenDeterminismTests(WorldCache cache) => _cache = cache;

    [Fact]
    public void SameSeed_ProducesIdenticalElevation()
    {
        var h1 = _cache.Get(TestSeed, variant: 0).World.HashElevation();
        var h2 = _cache.Get(TestSeed, variant: 1).World.HashElevation();
        Assert.Equal(h1, h2);
    }

    [Fact]
    public void SameSeed_ProducesIdenticalMoisture()
    {
        var h1 = _cache.Get(TestSeed, variant: 0).World.HashMoisture();
        var h2 = _cache.Get(TestSeed, variant: 1).World.HashMoisture();
        Assert.Equal(h1, h2);
    }

    [Fact]
    public void SameSeed_ProducesIdenticalTemperature()
    {
        var h1 = _cache.Get(TestSeed, variant: 0).World.HashTemperature();
        var h2 = _cache.Get(TestSeed, variant: 1).World.HashTemperature();
        Assert.Equal(h1, h2);
    }

    [Fact]
    public void SameSeed_ProducesIdenticalBiomes()
    {
        var h1 = _cache.Get(TestSeed, variant: 0).World.HashBiomes();
        var h2 = _cache.Get(TestSeed, variant: 1).World.HashBiomes();
        Assert.Equal(h1, h2);
    }

    [Fact]
    public void DifferentSeeds_ProduceDifferentElevation()
    {
        var h1 = _cache.Get(TestSeed).World.HashElevation();
        var h2 = _cache.Get(TestSeed + 1).World.HashElevation();
        Assert.NotEqual(h1, h2);
    }
}
