/**
 * Lumen Vale — three rooms, two gates, one vale.
 *
 * Room graph:   A "The Hollow"  ⇄  B "Crystal Grove"  ⇄  C "Sunken Shrine"
 *
 *  A: spawn, step platforms up to the AETHER WINGS, beetle patrol, the
 *     210px wings gate, and — past the gate — a sealed crystal vault holding
 *     the LUMEN SHARD (dash-breakable; the backtracking secret).
 *  B: reached through A's right door. Platforms climb to the EMBER DASH
 *     sigil. A floor-to-sky amber crystal barrier seals the way to C: no
 *     jump (single or double) clears it — only a dash shatters it.
 *  C: spike pit, shrine dais, and the exit portal.
 */

export const VIEW = { w: 960, h: 540 };
export const GROUND_Y = 470;

export type RoomId = 'A' | 'B' | 'C';

export interface Box { x: number; y: number; w: number; h: number }

export interface Solid extends Box {
  /** 'solid' blocks all sides; 'oneway' only catches downward feet */
  type: 'solid' | 'oneway';
  tile: 'ground' | 'platform';
  /** id => a dash-breakable crystal wall; gone once its id is in the broken set */
  breakable?: string;
}

export interface Door extends Box {
  to: RoomId;
  /** where the player appears in the target room */
  entry: { x: number; y: number };
}

export interface EnemySpawn {
  kind: 'beetle' | 'spore';
  min: number;
  max: number;
  /** beetle: ground line; spore: hover base line */
  y: number;
}

export interface RoomDef {
  id: RoomId;
  name: string;
  w: number;
  h: number;
  bg: 'background' | 'backgroundGrove' | 'backgroundShrine';
  /** rgb of the drifting fog layers */
  fog: [number, number, number];
  solids: Solid[];
  doors: Door[];
  enemies: EnemySpawn[];
  hazards: Box[];
  /** default spawn / respawn point for the room */
  entry: { x: number; y: number };
}

// pickups -------------------------------------------------------------------

export type PickupId = 'wings' | 'dash' | 'shard';

export interface PickupDef {
  id: PickupId;
  room: RoomId;
  x: number;
  y: number;
}

export const PICKUPS: PickupDef[] = [
  { id: 'wings', room: 'A', x: 238, y: 184 },
  { id: 'dash', room: 'B', x: 1060, y: 126 },
  // inside Room A's sealed vault — unreachable until the dash breaks it open
  { id: 'shard', room: 'A', x: 1105, y: 426 },
];

// gates ---------------------------------------------------------------------

/** the wings gate in A: jump-over wall, passable only with the double jump */
export const GATE = { x: 880, w: 64, topY: 260 };
/** the dash gate in B: collision extends far above the screen — nothing jumps it */
export const CRYSTAL_WALL = { id: 'gate-B', x: 1240, w: 46, topY: 60 };
/** the secret vault wall in A */
export const SECRET_WALL = { id: 'secret-A', x: 1000, w: 36, topY: 356 };

export const EXIT = { x: 1280, y: 430 }; // shrine portal, Room C, on the dais

// rooms ---------------------------------------------------------------------

const ROOM_W = 1440;
const ROOM_H = 540;

export const ROOMS: Record<RoomId, RoomDef> = {
  A: {
    id: 'A',
    name: 'The Hollow',
    w: ROOM_W,
    h: ROOM_H,
    bg: 'background',
    fog: [140, 180, 200],
    solids: [
      { x: -40, y: GROUND_Y, w: ROOM_W + 80, h: ROOM_H - GROUND_Y + 40, type: 'solid', tile: 'ground' },
      { x: 250, y: 380, w: 140, h: 16, type: 'oneway', tile: 'platform' },
      { x: 432, y: 300, w: 140, h: 16, type: 'oneway', tile: 'platform' },
      { x: 168, y: 228, w: 140, h: 16, type: 'oneway', tile: 'platform' },
      // the wings gate wall
      { x: GATE.x, y: GATE.topY, w: GATE.w, h: GROUND_Y - GATE.topY, type: 'solid', tile: 'ground' },
      // the sealed vault (past the gate): roof slab, solid right wall,
      // dash-breakable crystal left wall, shard inside
      { x: 1000, y: 330, w: 210, h: 26, type: 'solid', tile: 'platform' },
      { x: 1174, y: 356, w: 36, h: GROUND_Y - 356, type: 'solid', tile: 'ground' },
      { x: SECRET_WALL.x, y: SECRET_WALL.topY, w: SECRET_WALL.w, h: GROUND_Y - SECRET_WALL.topY, type: 'solid', tile: 'ground', breakable: SECRET_WALL.id },
    ],
    doors: [
      { x: 1422, y: 0, w: 18, h: ROOM_H, to: 'B', entry: { x: 52, y: GROUND_Y } },
    ],
    enemies: [{ kind: 'beetle', min: 600, max: 790, y: GROUND_Y }],
    hazards: [],
    entry: { x: 70, y: GROUND_Y },
  },

  B: {
    id: 'B',
    name: 'Crystal Grove',
    w: ROOM_W,
    h: ROOM_H,
    bg: 'backgroundGrove',
    fog: [130, 210, 225],
    solids: [
      { x: -40, y: GROUND_Y, w: ROOM_W + 80, h: ROOM_H - GROUND_Y + 40, type: 'solid', tile: 'ground' },
      { x: 380, y: 380, w: 140, h: 16, type: 'oneway', tile: 'platform' },
      { x: 600, y: 300, w: 140, h: 16, type: 'oneway', tile: 'platform' },
      { x: 830, y: 240, w: 140, h: 16, type: 'oneway', tile: 'platform' },
      { x: 980, y: 170, w: 160, h: 16, type: 'oneway', tile: 'platform' },
      // the amber crystal barrier: collision reaches far above the screen so
      // even a perfect double jump cannot clear it — dash-only
      { x: CRYSTAL_WALL.x, y: -300, w: CRYSTAL_WALL.w, h: GROUND_Y + 300, type: 'solid', tile: 'ground', breakable: CRYSTAL_WALL.id },
    ],
    doors: [
      { x: 0, y: 0, w: 18, h: ROOM_H, to: 'A', entry: { x: 1392, y: GROUND_Y } },
      { x: 1422, y: 0, w: 18, h: ROOM_H, to: 'C', entry: { x: 60, y: GROUND_Y } },
    ],
    enemies: [{ kind: 'spore', min: 380, max: 780, y: 290 }],
    hazards: [],
    entry: { x: 52, y: GROUND_Y },
  },

  C: {
    id: 'C',
    name: 'Sunken Shrine',
    w: ROOM_W,
    h: ROOM_H,
    bg: 'backgroundShrine',
    fog: [210, 170, 120],
    solids: [
      // two floor shelves with a spike pit between them
      { x: -40, y: GROUND_Y, w: 600, h: ROOM_H - GROUND_Y + 40, type: 'solid', tile: 'ground' },
      { x: 880, y: GROUND_Y, w: 600, h: ROOM_H - GROUND_Y + 40, type: 'solid', tile: 'ground' },
      // the shrine dais holding the portal
      { x: 1180, y: 430, w: 200, h: 40, type: 'solid', tile: 'platform' },
    ],
    doors: [
      { x: 0, y: 0, w: 18, h: ROOM_H, to: 'B', entry: { x: 1366, y: GROUND_Y } },
    ],
    enemies: [{ kind: 'spore', min: 920, max: 1150, y: 280 }],
    hazards: [{ x: 560, y: 514, w: 320, h: 26 }],
    entry: { x: 60, y: GROUND_Y },
  },
};

export const ROOM_ORDER: RoomId[] = ['A', 'B', 'C'];

// compatibility aliases for the v1 single-room API ---------------------------
export const ROOM = { w: ROOMS.A.w, h: ROOMS.A.h };
export const SPAWN = ROOMS.A.entry;
export const PICKUP_POS = { x: PICKUPS[0].x, y: PICKUPS[0].y };
export const ENEMY_PATROL = { min: 600, max: 790 };
