import type { NativeScannerDataStatus } from "../types/global";

export type IkScanCapabilityId = "artifacts" | "weapons" | "characters" | "materials";
export type IkScanCapabilityStatus = "native_capture" | "catalog_only" | "missing_data";

export interface IkScanCapabilityRow {
  id: IkScanCapabilityId;
  label: string;
  countLabel: string;
  status: IkScanCapabilityStatus;
  statusLabel: string;
  className: "ok" | "warn" | "muted";
  detail: string;
}

export function buildIkScanCapabilityRows(data: NativeScannerDataStatus | null | undefined): IkScanCapabilityRow[] {
  const valid = Boolean(data?.valid);
  return [
    capability({
      id: "artifacts",
      label: "Artifacts",
      countLabel: data ? `${data.artifactSets}/${data.artifactPieces}` : "0/0",
      valid,
      catalogAvailable: Boolean(data?.categories.artifacts.catalogAvailable ?? data?.valid),
      nativeCapture: Boolean(data?.categories.artifacts.nativeCaptureSupported ?? data?.categories.artifacts.supported),
      detail: "Native 16:9 visible-inventory capture path is implemented for artifact card crops.",
    }),
    capability({
      id: "weapons",
      label: "Weapons",
      countLabel: String(data?.weapons ?? 0),
      valid,
      catalogAvailable: Boolean(data?.categories.weapons.catalogAvailable ?? data?.valid),
      nativeCapture: Boolean(data?.categories.weapons.nativeCaptureSupported ?? false),
      detail: "IK weapon names and GOOD keys are loaded; weapon capture flow still needs category-specific evidence.",
    }),
    capability({
      id: "characters",
      label: "Characters",
      countLabel: String(data?.characters ?? 0),
      valid,
      catalogAvailable: Boolean(data?.categories.characters.catalogAvailable ?? data?.valid),
      nativeCapture: Boolean(data?.categories.characters.nativeCaptureSupported ?? false),
      detail: "IK character names and GOOD keys are loaded; character capture flow still needs category-specific evidence.",
    }),
    capability({
      id: "materials",
      label: "Materials",
      countLabel: String(data?.materials ?? 0),
      valid,
      catalogAvailable: Boolean(data?.categories.materials.catalogAvailable ?? data?.valid),
      nativeCapture: Boolean(data?.categories.materials.nativeCaptureSupported ?? false),
      detail: "IK material names and GOOD keys are loaded; material capture flow still needs category-specific evidence.",
    }),
  ];
}

function capability(input: {
  id: IkScanCapabilityId;
  label: string;
  countLabel: string;
  valid: boolean;
  catalogAvailable: boolean;
  nativeCapture: boolean;
  detail: string;
}): IkScanCapabilityRow {
  if (!input.valid || !input.catalogAvailable) {
    return {
      id: input.id,
      label: input.label,
      countLabel: input.countLabel,
      status: "missing_data",
      statusLabel: "Daten fehlen",
      className: "warn",
      detail: "IK inventorylists are incomplete or unavailable.",
    };
  }

  if (input.nativeCapture) {
    return {
      id: input.id,
      label: input.label,
      countLabel: input.countLabel,
      status: "native_capture",
      statusLabel: "Native Capture",
      className: "ok",
      detail: input.detail,
    };
  }

  return {
    id: input.id,
    label: input.label,
    countLabel: input.countLabel,
    status: "catalog_only",
    statusLabel: "Katalog bereit",
    className: "muted",
    detail: input.detail,
  };
}
