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

/**
 * Squad hall — hub of Level 1.
 *
 * Routes to corridor / kitchen / detective office / interview room.
 * Crank puzzle pieces still live here (records shutter).
 */

const RAMP_POINTS: Vec3[] = [
  { x: -1.4, y: 0, z: 1 },
  { x: 1.4, y: 0, z: 1 },
  { x: -1.4, y: 0, z: -1 },
  { x: 1.4, y: 0, z: -1 },
  { x: -1.4, y: 0.9, z: -1 },
  { x: 1.4, y: 0.9, z: -1 },
];

export const GALLERY: RoomDef = {
  id: "manor-gallery",
  displayName: "Ashgrove Precinct — Squad Hall",
  fog: { color: 0x0a0c10, near: 4, far: 24 },
  ambientLight: { color: 0x3a4050, intensity: 1 },
  lights: [
    {
      kind: "point",
      color: 0xffd9a0,
      intensity: 32,
      position: { x: 0, y: 2.6, z: 1 },
      distance: 16,
    },
    {
      kind: "directional",
      color: 0x404a60,
      intensity: 0.5,
      position: { x: 4, y: 8, z: 6 },
    },
  ],
  geometry: [
    floor("floor", { x: 0, y: 0, z: 0 }, 6, 5),
    // South: lobby door gap at x=0
    wall("wall-s-left", { x: -3.5, y: WALL_HALF, z: 5.15 }, { x: 2.5, y: WALL_HALF, z: WALL_THICKNESS }),
    wall("wall-s-right", { x: 3.5, y: WALL_HALF, z: 5.15 }, { x: 2.5, y: WALL_HALF, z: WALL_THICKNESS }),
    // East: office door gap at z=0
    wall("wall-e-north", { x: 6.15, y: WALL_HALF, z: -2.8 }, { x: WALL_THICKNESS, y: WALL_HALF, z: 2.2 }),
    wall("wall-e-south", { x: 6.15, y: WALL_HALF, z: 2.8 }, { x: WALL_THICKNESS, y: WALL_HALF, z: 2.2 }),
    // West: interview door gap at z=0
    wall("wall-w-north", { x: -6.15, y: WALL_HALF, z: -2.8 }, { x: WALL_THICKNESS, y: WALL_HALF, z: 2.2 }, STONE),
    wall("wall-w-south", { x: -6.15, y: WALL_HALF, z: 2.8 }, { x: WALL_THICKNESS, y: WALL_HALF, z: 2.2 }, STONE),
    // North: corridor gap at x=0, kitchen gap at x=-3.3
    wall("wall-n-far-left", { x: -5.1, y: WALL_HALF, z: -5.15 }, { x: 0.9, y: WALL_HALF, z: WALL_THICKNESS }),
    wall("wall-n-between", { x: -1.65, y: WALL_HALF, z: -5.15 }, { x: 0.85, y: WALL_HALF, z: WALL_THICKNESS }),
    wall("wall-n-right", { x: 3.5, y: WALL_HALF, z: -5.15 }, { x: 2.5, y: WALL_HALF, z: WALL_THICKNESS }),
    {
      id: "low-step",
      position: { x: -3, y: 0.15, z: 2 },
      visual: { kind: "box", size: { x: 2, y: 0.3, z: 2 }, color: TIMBER },
      colliders: [{ kind: "box", halfExtents: { x: 1, y: 0.15, z: 1 } }],
    },
    prop("crate", { x: 3, y: 0.4, z: 1.5 }, { x: 0.8, y: 0.8, z: 0.8 }, TIMBER),
    prop("bullpen-desk", { x: -3.5, y: 0.4, z: -1.5 }, { x: 1.6, y: 0.8, z: 0.9 }, TIMBER),
    {
      id: "ramp",
      position: { x: 4.5, y: 0, z: -2 },
      visual: { kind: "hull", points: RAMP_POINTS, color: 0x55504a },
      colliders: [{ kind: "hull", points: RAMP_POINTS }],
      material: { friction: 0.9 },
    },
    {
      id: "ledge",
      position: { x: 4.5, y: 0.45, z: -3.6 },
      visual: { kind: "box", size: { x: 2.8, y: 0.9, z: 1.2 }, color: 0x55504a },
      colliders: [{ kind: "box", halfExtents: { x: 1.4, y: 0.45, z: 0.6 } }],
    },
  ],
  cameraZones: [
    {
      id: "gallery",
      trigger: { center: { x: 0, y: 1.5, z: 0 }, halfExtents: { x: 6, y: 2, z: 5 } },
      camera: {
        position: { x: 5.2, y: 3.4, z: 4.2 },
        lookAt: { x: -0.5, y: 1, z: -1 },
        fov: 55,
        trackPlayer: true,
        trackRadius: 2.2,
      },
      blend: { mode: "cut" },
      priority: 0,
    },
  ],
  interactables: [
    {
      id: "portrait",
      label: "Precinct Portrait",
      position: { x: -0.5, y: 1.4, z: 4.6 },
      halfExtents: { x: 1.1, y: 1.4, z: 1.2 },
      action: {
        kind: "examine",
        text:
          "A water-damaged group photo of the night shift. One face has been scratched out — the same grey as the wall behind it.",
      },
      repeatable: true,
      visual: { kind: "box", size: { x: 0.8, y: 1.0, z: 0.1 }, color: 0x2e2a26 },
    },
    {
      id: "evidence-board",
      label: "Hall Board",
      position: { x: -3.5, y: 1.2, z: -1.5 },
      halfExtents: { x: 1.2, y: 1.2, z: 1.1 },
      action: {
        kind: "examine",
        text:
          "OPEN ROOMS: Lobby · Lounge · Interview · Detective Office · Corridor. SEALED: Records (valve) · Break Room (brass key) · Vault (cellar key). Question the living. Read the dead.",
      },
      repeatable: true,
      visual: { kind: "box", size: { x: 1.2, y: 0.9, z: 0.06 }, color: 0x3a3830 },
    },
    {
      id: "crank-handle-pickup",
      label: "Crank Handle",
      position: { x: 3, y: 0.95, z: 1.5 },
      halfExtents: { x: 1.1, y: 1.1, z: 1.1 },
      action: { kind: "pickup", itemId: "crank-handle" },
      visual: { kind: "box", size: { x: 0.35, y: 0.1, z: 0.1 }, color: 0x8a7f6d },
    },
    {
      id: "broken-shaft-pickup",
      label: "Broken Shaft",
      position: { x: 4.5, y: 1.05, z: -3.6 },
      halfExtents: { x: 1.2, y: 1.1, z: 1 },
      action: { kind: "pickup", itemId: "broken-shaft" },
      visual: { kind: "box", size: { x: 0.1, y: 0.1, z: 0.5 }, color: 0x6d6a63 },
    },
    door({
      id: "door-gallery-to-foyer",
      label: "Lobby",
      position: { x: 0, y: 1.1, z: 5 },
      targetRoomId: "manor-foyer",
      spawnPointId: "from-gallery",
    }),
    door({
      id: "door-gallery-to-corridor",
      label: "Corridor",
      position: { x: 0, y: 1.1, z: -5 },
      targetRoomId: "manor-corridor",
      spawnPointId: "from-gallery",
    }),
    door({
      id: "door-gallery-to-kitchen",
      label: "Break Room",
      position: { x: -3.3, y: 1.1, z: -5 },
      targetRoomId: "manor-kitchen",
      spawnPointId: "from-gallery",
      requiresItemId: "brass-key",
    }),
    door({
      id: "door-gallery-to-office",
      label: "Detective Office",
      position: { x: 6, y: 1.1, z: 0 },
      targetRoomId: "precinct-office",
      spawnPointId: "from-gallery",
    }),
    door({
      id: "door-gallery-to-interview",
      label: "Interview Room",
      position: { x: -6, y: 1.1, z: 0 },
      targetRoomId: "precinct-interview",
      spawnPointId: "from-gallery",
    }),
  ],
  spawnPoints: [
    { id: "from-foyer", position: { x: 0, y: 0, z: 3.5 }, yaw: Math.PI },
    { id: "from-corridor", position: { x: 0, y: 0, z: -3.5 }, yaw: 0 },
    { id: "from-kitchen", position: { x: -3.3, y: 0, z: -3.5 }, yaw: 0 },
    { id: "from-office", position: { x: 4.2, y: 0, z: 0 }, yaw: Math.PI / 2 },
    { id: "from-interview", position: { x: -4.2, y: 0, z: 0 }, yaw: -Math.PI / 2 },
    { id: "start", position: { x: 0, y: 0, z: 3.2 }, yaw: Math.PI },
  ],
};
