import { AVAILABLE_BOSS_IDS } from "../game/bossCatalog";
import type { BossId } from "../game/types";
import type { GroupBossCollection, ProfileStatId } from "./types";

export type ProfileSectionId = "roguelike" | "rogue-trials" | "hockey" | "hockey-pvp" | "blockbreaker" | "aether-assault" | "bosses";

export interface ProfileSectionDefinition {
  id: ProfileSectionId;
  label: string;
  copy: string;
  icon: string;
  statIds: readonly ProfileStatId[];
}

export const PROFILE_SECTIONS: readonly ProfileSectionDefinition[] = [
  { id: "roguelike", label: "Roguelike", copy: "Highest completed round", icon: "∞", statIds: ["roguelike"] },
  { id: "rogue-trials", label: "Trials Endless", copy: "Best endless boss run", icon: "Ⅲ", statIds: ["rogue-trials-endless"] },
  { id: "hockey", label: "Hockey", copy: "Returns and survival", icon: "◌", statIds: ["hockey-healing"] },
  { id: "hockey-pvp", label: "Hockey PVP", copy: "Record and race kills", icon: "◇", statIds: ["hockey-pvp-wins", "hockey-pvp-boss-kills"] },
  { id: "blockbreaker", label: "Blockbreaker", copy: "Score, bricks, survival", icon: "▦", statIds: ["blockbreaker-score", "blockbreaker-bricks", "blockbreaker-time"] },
  { id: "aether-assault", label: "Aether Assault", copy: "Score, wave, survival", icon: "⌁", statIds: ["aether-assault"] },
  { id: "bosses", label: "Bosses", copy: "Kills and boss pets", icon: "♛", statIds: [] },
] as const;

const BOSS_IDS = new Set<string>(AVAILABLE_BOSS_IDS);

export function isBossProfileStat(statId: ProfileStatId): statId is BossId {
  return BOSS_IDS.has(statId);
}

export function profileSectionForStat(statId: ProfileStatId): ProfileSectionId {
  if (isBossProfileStat(statId)) return "bosses";
  return PROFILE_SECTIONS.find((section) => section.statIds.includes(statId))?.id ?? "roguelike";
}

export function alphabeticalBosses(groups: readonly { bosses: readonly GroupBossCollection[] }[]): GroupBossCollection[] {
  return groups
    .flatMap((group) => group.bosses)
    .sort((left, right) => left.bossName.localeCompare(right.bossName));
}

export function defaultStatForSection(sectionId: ProfileSectionId, bosses: readonly GroupBossCollection[]): ProfileStatId {
  if (sectionId === "bosses") return bosses[0]?.bossId ?? "roguelike";
  return PROFILE_SECTIONS.find((section) => section.id === sectionId)?.statIds[0] ?? "roguelike";
}
