import type { InteractableDef, StaticGeometryDef, Vec3 } from "@psx/shared";

/** Shared materials for Ashgrove Precinct. */
export const STONE = 0x4a4f57;
export const PLASTER = 0x6b6357;
export const TIMBER = 0x4a3c2e;
export const FLOOR_COLOR = 0x3b3f45;
export const DOOR_COLOR = 0x5c4a3a;
export const METAL = 0x6a6e74;

export const WALL_HALF = 1.5;
export const WALL_THICKNESS = 0.15;
export const FLOOR_HALF = 0.15;

/** Wall slab: one box visual, one box collider. */
export function wall(
  id: string,
  center: Vec3,
  halfExtents: Vec3,
  color = PLASTER,
): StaticGeometryDef {
  return {
    id,
    position: center,
    visual: {
      kind: "box",
      size: { x: halfExtents.x * 2, y: halfExtents.y * 2, z: halfExtents.z * 2 },
      color,
    },
    colliders: [{ kind: "box", halfExtents }],
    material: { friction: 0.7 },
  };
}

export function floor(id: string, center: Vec3, halfX: number, halfZ: number): StaticGeometryDef {
  return wall(
    id,
    { x: center.x, y: -FLOOR_HALF, z: center.z },
    { x: halfX, y: FLOOR_HALF, z: halfZ },
    FLOOR_COLOR,
  );
}

/** Box prop with matching collider. */
export function prop(
  id: string,
  center: Vec3,
  size: Vec3,
  color: number,
): StaticGeometryDef {
  return {
    id,
    position: center,
    visual: { kind: "box", size, color },
    colliders: [
      {
        kind: "box",
        halfExtents: { x: size.x / 2, y: size.y / 2, z: size.z / 2 },
      },
    ],
  };
}

/** Door interactable — walk up and press E to transition rooms. */
export function door(options: {
  id: string;
  label: string;
  position: Vec3;
  targetRoomId: string;
  spawnPointId: string;
  requiresItemId?: string;
  requiresFlags?: string[];
  /** Visual slab; omit for an invisible trigger (e.g. open archway). */
  visual?: boolean;
  yawFacing?: number;
}): InteractableDef {
  return {
    id: options.id,
    label: options.label,
    position: options.position,
    halfExtents: { x: 1.1, y: 1.4, z: 1.1 },
    action: {
      kind: "door",
      targetRoomId: options.targetRoomId,
      spawnPointId: options.spawnPointId,
      ...(options.requiresItemId ? { requiresItemId: options.requiresItemId } : {}),
    },
    ...(options.requiresFlags ? { requiresFlags: options.requiresFlags } : {}),
    repeatable: true,
    visual:
      options.visual === false
        ? { kind: "none" }
        : { kind: "box", size: { x: 1.2, y: 2.2, z: 0.12 }, color: DOOR_COLOR },
  };
}

/** Sealed passage shown only while a flag is *not* set. */
export function sealedPassage(options: {
  id: string;
  label: string;
  position: Vec3;
  text: string;
  closedFlag: string;
}): InteractableDef {
  return {
    id: options.id,
    label: options.label,
    position: options.position,
    halfExtents: { x: 1.2, y: 1.4, z: 1.1 },
    action: { kind: "examine", text: options.text },
    requiresFlags: [`!${options.closedFlag}`],
    repeatable: true,
    visual: { kind: "box", size: { x: 1.4, y: 2.4, z: 0.18 }, color: 0x5c4a3a },
  };
}
