import type { GameSliceSession, SliceObjectiveId } from "./types";

export const SLICE_OBJECTIVES: Array<{ id: SliceObjectiveId; title: string; detail: string }> = [
  { id: "undock", title: "Undock", detail: "Launch from Jita IV - Moon 4." },
  { id: "navigate_to_belt", title: "Navigate to belt", detail: "Use Movement to reach Sol Asteroid Belt." },
  { id: "mine_ore", title: "Mine ore", detail: "Run one mining laser cycle for Veldspar." },
  { id: "dock_at_station", title: "Dock at station", detail: "Return to Jita IV - Moon 4 and dock." },
  { id: "refine_ore", title: "Refine ore", detail: "Convert Veldspar into minerals." },
  { id: "fit_module", title: "Fit module", detail: "Fit or confirm at least one module." },
  { id: "sell_goods", title: "Sell goods", detail: "Sell minerals or cargo on the market." },
  { id: "combat_trial", title: "Combat trial", detail: "Optional NPC bounty and loot branch." },
];

export const REQUIRED_OBJECTIVES: SliceObjectiveId[] = [
  "undock",
  "navigate_to_belt",
  "mine_ore",
  "dock_at_station",
  "refine_ore",
  "fit_module",
  "sell_goods",
];

export function getNextObjective(completedObjectives: SliceObjectiveId[]): SliceObjectiveId {
  return REQUIRED_OBJECTIVES.find((id) => !completedObjectives.includes(id)) ?? "combat_trial";
}

export function withCompletedObjective(session: GameSliceSession, objectiveId: SliceObjectiveId): GameSliceSession {
  const completedObjectives = session.completedObjectives.includes(objectiveId)
    ? session.completedObjectives
    : [...session.completedObjectives, objectiveId];
  return {
    ...session,
    completedObjectives,
    activeObjectiveId: getNextObjective(completedObjectives),
  };
}

export function getObjectiveProgress(session: GameSliceSession) {
  const requiredDone = REQUIRED_OBJECTIVES.filter((id) => session.completedObjectives.includes(id)).length;
  return {
    requiredDone,
    requiredTotal: REQUIRED_OBJECTIVES.length,
    percent: Math.round((requiredDone / REQUIRED_OBJECTIVES.length) * 100),
  };
}
