import { verdictMeta } from "../../common/verdictMeta";
import type { Artifact, Character, Recommendation } from "../../../types/domain";
import type { ReactNode } from "react";

export interface ArtifactRowModel {
  metaLabel: string;
  metaClassName: string;
  metaIcon: ReactNode;
  score: number;
  characterNames: string;
  artifactSummary: string;
  substatRows: string[];
}

interface UseArtifactRowModelInput {
  artifact: Artifact;
  recommendation?: Recommendation;
  characters: Character[];
  characterIds?: string[];
}

export function useArtifactRowModel({
  recommendation,
  characters,
  artifact,
  characterIds,
}: UseArtifactRowModelInput): ArtifactRowModel {
  const meta = verdictMeta[recommendation?.verdict ?? "needs_review"];
  const characterNames = !characterIds || characterIds.length === 0
    ? "Review first"
    : characterIds.map((id) => characters.find((character) => character.id === id)?.name ?? id).join(", ");
  const score = recommendation ? Math.round(recommendation.score) : 0;

  return {
    metaLabel: meta.label,
    metaClassName: meta.className,
    metaIcon: meta.icon,
    score,
    characterNames,
    artifactSummary: `${artifact.slot} - +${artifact.level} - ${artifact.mainStat}${artifact.equipped ? ` - ${artifact.equipped}` : ""}`,
    substatRows: artifact.substats.map((substat) => `${substat.key} ${substat.value}${substat.unit === "%" ? "%" : ""}`),
  };
}
