import { inferEvidenceFileKind } from "@/lib/evidenceFileKind";
import type { EvidenceBundle, EvidenceItemMeta } from "@/lib/applicationEvidenceApi";
import { EVIDENCE_KIND_ROWS } from "./applicationEvidenceConstants";

/** Aggregate 2.1 / 2.2 / kỹ thuật — cùng quy tắc với thẩm định từng dòng trong {@link ApplicationEvidenceDashboard}. */
export function deriveEvidenceVaultAggregateLabel(bundle: EvidenceBundle): {
  label: "Đạt" | "Không đạt" | "Chưa xem";
  textClassName: string;
} {
  const metas: EvidenceItemMeta[] = [];
  for (const { key } of EVIDENCE_KIND_ROWS) {
    const m = bundle[key];
    if (m) metas.push(m);
  }
  if (metas.length === 0) {
    return { label: "Chưa xem", textClassName: "text-foreground" };
  }
  if (metas.some((x) => x.reviewStatus === "rejected")) {
    return { label: "Không đạt", textClassName: "text-red-600 dark:text-red-400" };
  }
  if (metas.every((x) => x.reviewStatus === "approved")) {
    return { label: "Đạt", textClassName: "text-green-600 dark:text-green-400" };
  }
  return { label: "Chưa xem", textClassName: "text-foreground" };
}

export function inferKindFromMeta(m: EvidenceItemMeta) {
  const name = m.originalName || "";
  return inferEvidenceFileKind({
    type: m.mimeType || "",
    url: m.downloadUrl || m.viewUrl || m.storageKey || "",
    originalName: name,
  });
}

export function hasEvidenceAccessUrl(m: EvidenceItemMeta): boolean {
  return Boolean(m.downloadUrl || m.viewUrl);
}
