import type { BuildSuggestion } from "../../../types/domain";
import type { Artifact, Character } from "../../../types/domain";

export interface BuildCardModel {
  characterName: string;
  qualityLabel: string;
  roundedScore: number;
  explanation: string;
  hasWarnings: boolean;
  artifactRows: Array<{
    id: string;
    slot: string;
    setName: string;
    details: string;
  }>;
  warnings: string[];
}

interface UseBuildCardModelInput {
  build: BuildSuggestion;
  characters: Character[];
  artifactLookup: Map<string, Artifact>;
}

export function useBuildCardModel({
  build,
  characters,
  artifactLookup,
}: UseBuildCardModelInput): BuildCardModel {
  const character = characters.find((entry) => entry.id === build.characterId);
  const artifacts = build.artifactIds
    .map((id) => artifactLookup.get(id))
    .filter((artifact): artifact is Artifact => Boolean(artifact));

  return {
    characterName: character?.name ?? build.characterId,
    qualityLabel: build.quality.replace("_", " "),
    roundedScore: Math.round(build.score),
    explanation: build.explanation,
    hasWarnings: build.warnings.length > 0,
    artifactRows: artifacts.map((artifact) => ({
      id: artifact.id,
      slot: artifact.slot,
      setName: artifact.setName,
      details: `+${artifact.level} - ${artifact.mainStat}${artifact.equipped ? ` - ${artifact.equipped}` : ""}`,
    })),
    warnings: build.warnings,
  };
}
