import type { SliceTarget, SliceVec3 } from "./types";

export const SLICE_SYSTEM_ID = "sol";

export const SLICE_STATION_POSITION: SliceVec3 = [-10, 0, 10];
export const SLICE_BELT_POSITION: SliceVec3 = [26, 0, -8];
export const SLICE_OPEN_SPACE_POSITION: SliceVec3 = [0, 0, 0];

export const SLICE_STATION = {
  systemId: SLICE_SYSTEM_ID,
  poiId: "sol-station-0",
  name: "Jita IV - Moon 4",
} as const;

export const SLICE_BELT = {
  systemId: SLICE_SYSTEM_ID,
  poiId: "sol-belt-0",
  name: "Sol Asteroid Belt",
} as const;

export const SLICE_DURATIONS = {
  undock: 1000,
  localTravel: 1800,
  docking: 1000,
  mining: 1200,
} as const;

export const SLICE_POI_NAMES: Record<string, string> = {
  [SLICE_STATION.poiId]: SLICE_STATION.name,
  [SLICE_BELT.poiId]: SLICE_BELT.name,
};

export const SLICE_TRAVEL_SYSTEM_IDS = ["sol", "amarr", "heinoo", "rens", "dodixie"] as const;

export function getSystemIdFromPoiId(poiId: string | null | undefined, fallback = SLICE_SYSTEM_ID): string {
  if (!poiId) return fallback;
  const match = /^(.+)-(station|belt|stargate|anomaly)/.exec(poiId);
  return match?.[1] ?? fallback;
}

export function isStationPoi(poiId: string | null | undefined): boolean {
  return Boolean(poiId?.includes("-station-"));
}

export function isBeltPoi(poiId: string | null | undefined): boolean {
  return Boolean(poiId?.includes("-belt-"));
}

export function readablePoiName(poiId: string | null | undefined): string {
  if (!poiId) return "Open space";
  return SLICE_POI_NAMES[poiId] ?? poiId.replace(/-/g, " ").replace(/\b\w/g, (char) => char.toUpperCase());
}

export const SLICE_POI_TARGETS: Record<string, SliceTarget> = {
  [SLICE_STATION.poiId]: {
    id: SLICE_STATION.poiId,
    name: SLICE_STATION.name,
    kind: "station",
    systemId: SLICE_STATION.systemId,
    poiId: SLICE_STATION.poiId,
    position: SLICE_STATION_POSITION,
  },
  [SLICE_BELT.poiId]: {
    id: SLICE_BELT.poiId,
    name: SLICE_BELT.name,
    kind: "asteroid_belt",
    systemId: SLICE_BELT.systemId,
    poiId: SLICE_BELT.poiId,
    position: SLICE_BELT_POSITION,
  },
};

export function getSlicePoiTarget(poiId: string | null | undefined): SliceTarget | null {
  return poiId ? SLICE_POI_TARGETS[poiId] ?? null : null;
}
