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

namespace Theriapolis.Tests.Worldgen;

/// <summary>
/// Macro-template elevation and moisture constraints must hold after generation.
/// Tests from the Phase 1 acceptance criteria.
/// </summary>
public sealed class MacroConstraintTests : IClassFixture<WorldCache>
{
    private const ulong TestSeed = 0xCAFEBABEUL;
    private readonly WorldCache _cache;

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

    [Fact]
    public void Mountain_Cells_HaveElevation_AboveFloor()
    {
        var ctx = _cache.Get(TestSeed);
        int W = C.WORLD_WIDTH_TILES, H = C.WORLD_HEIGHT_TILES;
        int violations = 0;

        for (int ty = 0; ty < H; ty += 4)
        for (int tx = 0; tx < W; tx += 4)
        {
            ref var tile = ref ctx.World.TileAt(tx, ty);
            var cell = ctx.World.MacroCellForTile(tile);
            // Skip ocean tiles — they're forced below sea level regardless of macro constraint
            if (tile.Biome == BiomeId.Ocean) continue;
            if (cell.BiomeType.Contains("mountain", StringComparison.OrdinalIgnoreCase))
            {
                if (tile.Elevation < cell.ElevationFloor - 0.05f) // 5% tolerance for blending
                    violations++;
            }
        }
        Assert.Equal(0, violations);
    }

    [Fact]
    public void Grassland_Cells_HaveElevation_BelowCeiling()
    {
        var ctx = _cache.Get(TestSeed);
        int W = C.WORLD_WIDTH_TILES, H = C.WORLD_HEIGHT_TILES;
        int violations = 0;

        for (int ty = 0; ty < H; ty += 4)
        for (int tx = 0; tx < W; tx += 4)
        {
            ref var tile = ref ctx.World.TileAt(tx, ty);
            if (tile.Biome == BiomeId.Ocean) continue;
            var cell = ctx.World.MacroCellForTile(tile);
            if (cell.BiomeType.Contains("grassland", StringComparison.OrdinalIgnoreCase))
            {
                if (tile.Elevation > cell.ElevationCeiling + 0.05f)
                    violations++;
            }
        }
        Assert.Equal(0, violations);
    }

    [Fact]
    public void Tundra_Cells_HaveMoisture_BelowCeiling()
    {
        var ctx = _cache.Get(TestSeed);
        int W = C.WORLD_WIDTH_TILES, H = C.WORLD_HEIGHT_TILES;
        int violations = 0;

        for (int ty = 0; ty < H; ty += 4)
        for (int tx = 0; tx < W; tx += 4)
        {
            ref var tile = ref ctx.World.TileAt(tx, ty);
            if (tile.Biome == BiomeId.Ocean) continue;
            var cell = ctx.World.MacroCellForTile(tile);
            if (cell.BiomeType.Contains("tundra", StringComparison.OrdinalIgnoreCase))
            {
                if (tile.Moisture > cell.MoistureCeiling + 0.05f)
                    violations++;
            }
        }
        Assert.Equal(0, violations);
    }

    [Fact]
    public void Subtropical_Cells_HaveMoisture_AboveFloor()
    {
        var ctx = _cache.Get(TestSeed);
        int W = C.WORLD_WIDTH_TILES, H = C.WORLD_HEIGHT_TILES;
        int violations = 0;

        for (int ty = 0; ty < H; ty += 4)
        for (int tx = 0; tx < W; tx += 4)
        {
            ref var tile = ref ctx.World.TileAt(tx, ty);
            if (tile.Biome == BiomeId.Ocean) continue;
            var cell = ctx.World.MacroCellForTile(tile);
            if (cell.BiomeType.Contains("subtropical", StringComparison.OrdinalIgnoreCase))
            {
                if (tile.Moisture < cell.MoistureFloor - 0.05f)
                    violations++;
            }
        }
        Assert.Equal(0, violations);
    }
}
