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

describe("System: Procedural Generation (Crystal Spires)", () => {
  let grid;

  beforeEach(() => {
    // Taller grid for verticality
    grid = new VoxelGrid(40, 20, 40);
  });

  it("CoA 1: Should generate vertical columns (Pillars)", () => {
    const gen = new CrystalSpiresGenerator(grid, 12345);
    gen.generate(1, 0); // 1 Spire, 0 Floors (just pillar)

    // Check if the pillar goes from bottom to top
    let solidColumnFound = false;

    // Scan full grid
    for (let x = 0; x < 40; x++) {
      for (let z = 0; z < 40; z++) {
        if (grid.getCell(x, 0, z) !== 0 && grid.getCell(x, 19, z) !== 0) {
          solidColumnFound = true;
          break;
        }
      }
    }

    expect(solidColumnFound).to.be.true;
  });

  it("CoA 2: Should generate platform discs at specific heights", () => {
    const gen = new CrystalSpiresGenerator(grid, 999);
    // 1 Spire, 2 Floors, Spacing 5 => Floors at y=4 and y=9
    gen.generate(1, 2, 5);

    let platform1Count = 0;
    let platform2Count = 0;

    for (let x = 0; x < 40; x++) {
      for (let z = 0; z < 40; z++) {
        if (grid.getCell(x, 4, z) === 1) platform1Count++;
        if (grid.getCell(x, 9, z) === 1) platform2Count++;
      }
    }

    // A disc of radius 4 has area ~50.
    expect(platform1Count).to.be.greaterThan(10);
    expect(platform2Count).to.be.greaterThan(10);
  });

  it("CoA 3: Should generate bridges (ID 20)", () => {
    const gen = new CrystalSpiresGenerator(grid, 12345);
    // 2 Spires, 2 Floors => Should connect them
    gen.generate(2, 2, 5);

    let bridgeCount = 0;
    for (let i = 0; i < grid.cells.length; i++) {
      if (grid.cells[i] === 20) bridgeCount++;
    }

    expect(bridgeCount).to.be.greaterThan(0);
  });

  it("CoA 4: Should scatter crystal cover (ID 15)", () => {
    const gen = new CrystalSpiresGenerator(grid, 12345);
    gen.generate(2, 2, 5); // Should produce some platforms

    let crystalCount = 0;
    for (let i = 0; i < grid.cells.length; i++) {
      if (grid.cells[i] === 15) crystalCount++;
    }

    // 5% density on platforms should yield at least a few crystals
    expect(crystalCount).to.be.greaterThan(0);
  });
});
