import type { ParsedArtifactCandidate } from "./artifactOcrParser";
import { shouldSaveReviewSample } from "./scannerLearning";
import type { CaptureResult } from "../types/global";

export interface ReviewReasonInput {
  capture: CaptureResult;
  parsed: ParsedArtifactCandidate;
}

export function getAutoReviewReason(capture: ReviewReasonInput["capture"], parsed: ReviewReasonInput["parsed"]) {
  if (!capture.detailDataUrl || !capture.crops?.length || !capture.ocr?.length) return "missing-crops-or-ocr";
  if (!shouldSaveReviewSample(parsed)) return "";
  const lowFields = Object.entries(parsed.fields)
    .filter(([, field]) => field.confidence < 70)
    .map(([key]) => key);
  if (parsed.confidence < 82) return `low-total-confidence-${parsed.confidence}`;
  if (lowFields.length > 0) return `low-field-${lowFields.join("-")}`;
  if (parsed.notes.some((note) => /not confidently|incomplete|low/i.test(note))) return "parser-notes";
  return "";
}

export function wait(ms: number) {
  return new Promise((resolve) => window.setTimeout(resolve, ms));
}
