import { expect } from "@esm-bundle/chai";
import { RustedFloorTextureGenerator } from "../../src/generation/textures/RustedFloorTextureGenerator.js";

describe("System: Procedural Textures (Rusted Floor)", () => {
  it("CoA 1: Should generate valid OffscreenCanvas maps for all PBR channels", () => {
    const gen = new RustedFloorTextureGenerator(123);
    const maps = gen.generateFloor(64);

    expect(maps.diffuse).to.be.instanceOf(OffscreenCanvas);
    expect(maps.normal).to.be.instanceOf(OffscreenCanvas);
    expect(maps.roughness).to.be.instanceOf(OffscreenCanvas);
    expect(maps.bump).to.be.instanceOf(OffscreenCanvas);

    expect(maps.diffuse.width).to.equal(64);
  });

  it("CoA 2: Should populate pixel data with expected colors", () => {
    const gen = new RustedFloorTextureGenerator(123);
    const maps = gen.generateFloor(64);
    const ctx = maps.diffuse.getContext("2d");
    const data = ctx.getImageData(0, 0, 64, 64).data;

    // Check center pixel - likely metal or rust
    // Moving check off-center to avoid Seams (which are dark/black)
    const sampleIndex = (36 * 64 + 36) * 4;
    expect(data[sampleIndex + 3]).to.equal(255); // Opaque
    expect(data[sampleIndex]).to.be.within(20, 200); // Valid color range
  });

  it("CoA 3: Should generate variant patterns with different seeds", () => {
    const gen = new RustedFloorTextureGenerator(123);

    // Generate two maps with different variant seeds
    const map1 = gen.generateFloor(64, 111);
    const map2 = gen.generateFloor(64, 222);

    // Sample off-center (36, 36) to ensure we hit the metal plate, not the seam.
    // 32 is a multiple of 16 (tile size), so 32,32 is a seam (deterministic).
    // 36 is inside the plate where noise applies.
    const data1 = map1.diffuse.getContext("2d").getImageData(36, 36, 1, 1).data;
    const data2 = map2.diffuse.getContext("2d").getImageData(36, 36, 1, 1).data;

    // Pixels should differ due to rust noise variation
    const isIdentical = data1[0] === data2[0] && data1[1] === data2[1];

    expect(isIdentical).to.be.false;
  });
});
