import { expect } from "@esm-bundle/chai";
import { VoxelGrid } from "../../src/grid/VoxelGrid.js";
import { VoidSeepDepthsGenerator } from "../../src/generation/VoidSeepDepthsGenerator.js";

// MOCK Browser APIs if missing (though WTR usually provides browser env)
// But OffscreenCanvas might need partial mock if headless browser lacks full support or for consistency
if (typeof window !== "undefined" && !window.OffscreenCanvas) {
  class MockCanvas {
    constructor(width, height) {
      this.width = width;
      this.height = height;
    }
    getContext(type) {
      return {
        createImageData: (w, h) => ({ data: new Uint8ClampedArray(w * h * 4) }),
        putImageData: () => {},
        drawImage: () => {},
        fillStyle: "",
        fillRect: () => {},
        strokeStyle: "",
        beginPath: () => {},
        moveTo: () => {},
        lineTo: () => {},
        stroke: () => {},
        bezierCurveTo: () => {},
        createRadialGradient: () => ({ addColorStop: () => {} }),
      };
    }
    transferToImageBitmap() {
      return {};
    }
  }
  window.OffscreenCanvas = MockCanvas;
}

describe("Generation: VoidSeepDepthsGenerator", () => {
  it("should generate a void seep map with correct IDs", () => {
    const grid = new VoxelGrid(40, 20, 40);
    const generator = new VoidSeepDepthsGenerator(grid, 12345);
    generator.generate();

    let solidCount = 0;
    let obsidianCount = 0;
    let corruptionCount = 0;
    let glowingPillarCount = 0;

    for (let x = 0; x < grid.size.x; x++) {
      for (let y = 0; y < grid.size.y; y++) {
        for (let z = 0; z < grid.size.z; z++) {
          const id = grid.getCell(x, y, z);
          if (id !== 0) {
            solidCount++;
            if (id === 50 || id === 52) obsidianCount++;
            if (id === 51 || id === 55) corruptionCount++;
            if (id === 53) glowingPillarCount++;
          }
        }
      }
    }

    expect(solidCount).to.be.greaterThan(0, "Should generate solid voxels");
    expect(obsidianCount).to.be.greaterThan(
      0,
      "Should generate obsidian (50, 52)"
    );
    expect(corruptionCount).to.be.greaterThan(
      0,
      "Should generate corruption (51, 55)"
    );

    // Difficulty 1 with seed 12345 verified to have pillars
    expect(glowingPillarCount).to.be.greaterThan(
      0,
      "Should generate glowing pillars (53) with seed 12345"
    );
  });

  it("should have start and end zones", () => {
    const grid = new VoxelGrid(40, 20, 40);
    // Use a seed that doesn't put a huge rift exactly at start logic if randomly unlikely
    const generator = new VoidSeepDepthsGenerator(grid, 99999);
    generator.generate();

    const cx = 20;

    // Start Zone (North, Z min)
    let startSolid = false;
    for (let x = cx - 2; x <= cx + 2; x++) {
      for (let z = 1; z <= 5; z++) {
        if (grid.getCell(x, 5, z) !== 0) startSolid = true;
      }
    }
    expect(startSolid).to.be.true;

    // End Zone (South, Z max)
    let endSolid = false;
    for (let x = cx - 2; x <= cx + 2; x++) {
      for (let z = 35; z <= 38; z++) {
        if (grid.getCell(x, 5, z) !== 0) endSolid = true;
      }
    }
    expect(endSolid).to.be.true;
  });
});
