import { expect } from "@esm-bundle/chai";
import * as THREE from "three";
import { GameLoop } from "../../../src/core/GameLoop.js";
import {
  createGameLoopSetup,
  cleanupGameLoop,
  createRunData,
} from "./helpers.js";

describe("Core: GameLoop - Initialization", function () {
  this.timeout(30000);

  let gameLoop;
  let container;

  beforeEach(() => {
    const setup = createGameLoopSetup();
    gameLoop = setup.gameLoop;
    container = setup.container;
  });

  afterEach(() => {
    cleanupGameLoop(gameLoop, container);
  });

  it("CoA 1: init() should setup Three.js scene, camera, and renderer", () => {
    gameLoop.init(container);

    expect(gameLoop.scene).to.be.instanceOf(THREE.Scene);
    expect(gameLoop.camera).to.be.instanceOf(THREE.PerspectiveCamera);
    expect(gameLoop.renderer).to.be.instanceOf(THREE.WebGLRenderer);

    // Verify renderer is attached to DOM
    expect(container.querySelector("canvas")).to.exist;
  });

  it("CoA 2: startLevel() should initialize grid, visuals, and generate world", async () => {
    gameLoop.init(container);

    const runData = createRunData();

    await gameLoop.startLevel(runData, { startAnimation: false });

    // Grid should be populated
    expect(gameLoop.grid).to.exist;
    // Check center of map (likely not empty for RuinGen) or at least check valid bounds
    expect(gameLoop.grid.size.x).to.be.greaterThan(0);

    // VoxelManager should be initialized
    expect(gameLoop.voxelManager).to.exist;
    // Should have visual meshes
    expect(gameLoop.scene.children.length).to.be.greaterThan(0);
  });
});

