import type { RoomDef } from "@psx/shared";
import { door, floor, STONE, wall, WALL_HALF, WALL_THICKNESS } from "./helpers.js";

/**
 * Narrow corridor — valve puzzle opens the study.
 *
 * The north shutter is flag-gated geometry. Until `study-shutter-open` is set,
 * examining it explains the puzzle; after the valve turns, the shutter
 * despawns and the study door becomes active.
 */
export const CORRIDOR: RoomDef = {
  id: "manor-corridor",
  displayName: "Ashgrove Precinct — Corridor",
  fog: { color: 0x080a0e, near: 2.5, far: 14 },
  ambientLight: { color: 0x303848, intensity: 0.85 },
  lights: [
    {
      kind: "point",
      color: 0xa8c0ff,
      intensity: 22,
      position: { x: 0, y: 2.4, z: 0 },
      distance: 12,
    },
  ],
  geometry: [
    floor("floor", { x: 0, y: 0, z: 0 }, 1.6, 5),
    wall("wall-w", { x: -1.75, y: WALL_HALF, z: 0 }, { x: WALL_THICKNESS, y: WALL_HALF, z: 5 }, STONE),
    wall("wall-e", { x: 1.75, y: WALL_HALF, z: 0 }, { x: WALL_THICKNESS, y: WALL_HALF, z: 5 }, STONE),
    wall("wall-s-left", { x: -1.05, y: WALL_HALF, z: 5.15 }, { x: 0.55, y: WALL_HALF, z: WALL_THICKNESS }),
    wall("wall-s-right", { x: 1.05, y: WALL_HALF, z: 5.15 }, { x: 0.55, y: WALL_HALF, z: WALL_THICKNESS }),
    wall("wall-n-left", { x: -1.05, y: WALL_HALF, z: -5.15 }, { x: 0.55, y: WALL_HALF, z: WALL_THICKNESS }),
    wall("wall-n-right", { x: 1.05, y: WALL_HALF, z: -5.15 }, { x: 0.55, y: WALL_HALF, z: WALL_THICKNESS }),
    {
      id: "study-shutter",
      position: { x: 0, y: WALL_HALF, z: -5 },
      visual: { kind: "box", size: { x: 1.4, y: 3, z: 0.2 }, color: 0x5c4a3a },
      colliders: [{ kind: "box", halfExtents: { x: 0.7, y: WALL_HALF, z: 0.1 } }],
      removeOnFlag: "study-shutter-open",
    },
  ],
  cameraZones: [
    {
      id: "corridor",
      trigger: { center: { x: 0, y: 1.5, z: 0 }, halfExtents: { x: 1.6, y: 2, z: 5 } },
      camera: {
        position: { x: 1.45, y: 2.9, z: 3.8 },
        lookAt: { x: 0, y: 1, z: -2 },
        fov: 62,
      },
      blend: { mode: "cut" },
      priority: 0,
    },
  ],
  interactables: [
    {
      id: "valve",
      label: "Valve",
      position: { x: 1.1, y: 1.1, z: -3.2 },
      halfExtents: { x: 1.2, y: 1.3, z: 1.2 },
      action: {
        kind: "useItem",
        acceptsItemId: "repaired-crank",
        onUseFlag: "study-shutter-open",
        text: "A valve stem with a square socket. Your fingers find no purchase — it needs a crank.",
      },
      visual: { kind: "box", size: { x: 0.3, y: 0.3, z: 0.15 }, color: 0x7a3b2e },
    },
    door({
      id: "door-corridor-to-gallery",
      label: "Squad Hall",
      position: { x: 0, y: 1.1, z: 5 },
      targetRoomId: "manor-gallery",
      spawnPointId: "from-corridor",
    }),
    {
      id: "study-shutter-examine",
      label: "Steel Shutter",
      position: { x: 0, y: 1.1, z: -4.6 },
      halfExtents: { x: 1.2, y: 1.4, z: 1 },
      action: {
        kind: "examine",
        text: "A steel shutter seals Records. Pipes along the wall lead to a valve — same hardware the night shift used as a lock.",
      },
      requiresFlags: ["!study-shutter-open"],
      repeatable: true,
      visual: { kind: "none" },
    },
    door({
      id: "door-corridor-to-study",
      label: "Records",
      position: { x: 0, y: 1.1, z: -5 },
      targetRoomId: "manor-study",
      spawnPointId: "from-corridor",
      requiresFlags: ["study-shutter-open"],
      visual: false,
    }),
  ],
  spawnPoints: [
    { id: "from-gallery", position: { x: 0, y: 0, z: 3.5 }, yaw: Math.PI },
    { id: "from-study", position: { x: 0, y: 0, z: -3.5 }, yaw: 0 },
  ],
};
