import { useCallback } from "react";
import type { ScanMainSectionProps } from "../types";

export interface ScanMainSectionModel {
  canOpenDetails: boolean;
  handleOpenDetails: () => void;
  handleOpenReviewQueue: () => void;
  captureImageSrc: string;
  captureImageAlt: string;
  hasCapture: boolean;
  captureModeText: string;
  resultHeading: string;
  noArtifactText: string;
  noCaptureMessage: string;
  targetLabel: string;
  dbLabel: string;
  reviewLabel: string;
  rulesLabel: string;
}

type UseScanMainSectionModelProps = Pick<
  ScanMainSectionProps,
  | "latestCapture"
  | "parsedArtifact"
  | "activeTargetCount"
  | "storedTotal"
  | "reviewSampleTotal"
  | "learningRulesLoaded"
  | "learningRuleCount"
  | "setDetailsOpen"
  | "openReviewQueue"
>;

export function useScanMainSectionModel({
  latestCapture,
  parsedArtifact,
  activeTargetCount,
  storedTotal,
  reviewSampleTotal,
  learningRulesLoaded,
  learningRuleCount,
  setDetailsOpen,
  openReviewQueue,
}: UseScanMainSectionModelProps): ScanMainSectionModel {
  const handleOpenDetails = useCallback(() => {
    setDetailsOpen(true);
  }, [setDetailsOpen]);
  const handleOpenReviewQueue = useCallback(() => {
    void openReviewQueue();
  }, [openReviewQueue]);
  const canOpenDetails = Boolean(latestCapture?.crops?.length || latestCapture?.ocr?.length);
  const captureImageSrc = latestCapture ? latestCapture.detailDataUrl ?? latestCapture.dataUrl : "";
  const captureImageAlt = latestCapture
    ? `Latest capture from ${latestCapture.name}`
    : "Latest capture is not available yet";
  const captureModeText = latestCapture ? "Erkannt" : "Warte";
  const resultHeading = parsedArtifact ? parsedArtifact.name : "Noch kein Artifact";
  const noArtifactText = "Oeffne ein Artifact in Genshin und nutze \"Einzelnes Artifact lesen\" - oder starte direkt den Auto-Scan.";
  const noCaptureMessage = noArtifactText;
  const targetLabel = `Ziel ${activeTargetCount}`;
  const dbLabel = `DB ${storedTotal ?? "-"}`;
  const reviewLabel = `Review ${reviewSampleTotal}`;
  const rulesLabel = `Regeln ${learningRulesLoaded ? learningRuleCount : "..."}`;

  return {
    canOpenDetails,
    handleOpenDetails,
    handleOpenReviewQueue,
    captureImageSrc,
    captureImageAlt,
    hasCapture: Boolean(latestCapture),
    captureModeText,
    resultHeading,
    noArtifactText,
    noCaptureMessage,
    targetLabel,
    dbLabel,
    reviewLabel,
    rulesLabel,
  };
}
