import type { ArtifactVerdict, AppSnapshot } from "../../../types/domain";
import { summarizeSnapshot } from "../../../lib/snapshotSummary";
import type { AppMetricCard } from "../../layout/types";

const verdictLabelByType: Record<ArtifactVerdict, string> = {
  keep: "Keep",
  maybe_level: "Test level",
  character_specific: "Character-specific",
  trash_candidate: "Trash",
  needs_review: "Needs review",
};

export interface AppTopbarSafetyCounts {
  keep: number;
  maybe: number;
  trash: number;
  review: number;
}

export interface AppMetricsModel {
  topbarSafety: AppTopbarSafetyCounts;
  cards: AppMetricCard[];
}

export function createAppMetrics(snapshot: AppSnapshot): AppMetricsModel {
  const summary = summarizeSnapshot(snapshot);
  const recommendations = snapshot.recommendations;
  const topbarSafety: AppTopbarSafetyCounts = {
    keep: recommendations.filter((entry: { verdict: ArtifactVerdict }) => entry.verdict === "keep").length,
    maybe: recommendations.filter((entry: { verdict: ArtifactVerdict }) => entry.verdict === "maybe_level").length,
    trash: recommendations.filter((entry: { verdict: ArtifactVerdict }) => entry.verdict === "trash_candidate").length,
    review: recommendations.filter((entry: { verdict: ArtifactVerdict }) => entry.verdict === "needs_review").length,
  };

  const cards: AppMetricCard[] = [
    { label: verdictLabelByType.keep, value: summary.artifacts, detail: `${topbarSafety.review} brauchen Review` },
    { label: "Behalten", value: summary.useful, detail: `${topbarSafety.keep + topbarSafety.maybe} / ${snapshot.artifacts.length} usable` },
    { label: "Trash-Kandidaten", value: summary.trash, detail: "wird nie automatisch geloescht" },
    { label: "Build-Optionen", value: summary.builds, detail: "Top-Vorschlaege verfuegbar" },
  ];

  return { topbarSafety, cards };
}
