#if TOOLS
using Godot;

namespace Mr.BrickAdventures.Tools.CaLevelGenerator;

[Tool]
public partial class CaGeneratorTestRunner : Node
{
    public override void _Ready()
    {
        int pass = 0, fail = 0;

        void Assert(bool cond, string msg)
        {
            if (cond) { GD.Print($"  PASS: {msg}"); pass++; }
            else { GD.PrintErr($"  FAIL: {msg}"); fail++; }
        }

        GD.Print("=== CaGenerator Tests ===");

        // Grid dimensions
        var s = new CaGeneratorSettings { Width = 20, Height = 15, SmoothingPasses = 0, Seed = 42 };
        var g = CaGenerator.Generate(s);
        Assert(g.GetLength(0) == 20, "Width == 20");
        Assert(g.GetLength(1) == 15, "Height == 15");

        // Determinism
        var g2 = CaGenerator.Generate(s);
        bool det = true;
        for (int x = 0; x < 20 && det; x++)
            for (int y = 0; y < 15 && det; y++)
                if (g[x, y] != g2[x, y]) det = false;
        Assert(det, "Same seed → same output");

        // Different seeds → different output
        var s2 = new CaGeneratorSettings { Width = 20, Height = 15, SmoothingPasses = 0, Seed = 99 };
        var g3 = CaGenerator.Generate(s2);
        bool diff = false;
        for (int x = 0; x < 20 && !diff; x++)
            for (int y = 0; y < 15 && !diff; y++)
                if (g[x, y] != g3[x, y]) diff = true;
        Assert(diff, "Different seeds → different output");

        // Cave border walls
        var sb = new CaGeneratorSettings { Width = 20, Height = 15, BorderWalls = true, SmoothingPasses = 0, Seed = 42 };
        var gb = CaGenerator.Generate(sb);
        bool border = true;
        for (int x = 0; x < 20; x++) if (!gb[x, 0] || !gb[x, 14]) { border = false; break; }
        for (int y = 0; y < 15; y++) if (!gb[0, y] || !gb[19, y]) { border = false; break; }
        Assert(border, "Cave: border cells all solid");

        // Platform floor
        var sp = new CaGeneratorSettings { Mode = CaMode.Platform, Width = 20, Height = 15, SmoothingPasses = 0, Seed = 42 };
        var gp = CaGenerator.Generate(sp);
        bool floor = true;
        for (int x = 0; x < 20; x++) if (!gp[x, 14] || !gp[x, 13]) { floor = false; break; }
        Assert(floor, "Platform: bottom 2 rows solid");

        // Platform: top row not all solid (mostly empty at top)
        int topSolid = 0;
        for (int x = 0; x < 20; x++) if (gp[x, 0]) topSolid++;
        Assert(topSolid < 15, "Platform: top row mostly empty");

        // Terrain: bottom always solid
        var st = new CaGeneratorSettings { Mode = CaMode.Terrain, Width = 20, Height = 15, SmoothingPasses = 0, Seed = 42 };
        var gt = CaGenerator.Generate(st);
        bool terrBottom = true;
        for (int x = 0; x < 20; x++) if (!gt[x, 14]) { terrBottom = false; break; }
        Assert(terrBottom, "Terrain: bottom row always solid");

        // Smooth: output same dimensions
        var smoothed = CaGenerator.Smooth(g);
        Assert(smoothed.GetLength(0) == 20, "Smooth: width preserved");
        Assert(smoothed.GetLength(1) == 15, "Smooth: height preserved");

        GD.Print($"=== Results: {pass} passed, {fail} failed ===");
    }
}
#endif
