import { shouldFlagArtifactForReview } from "../../../lib/scannerLearning";
import type { ScannerLearningRules } from "../../../lib/scannerLearning";
import type { ParsedArtifactCandidate } from "../../../lib/artifactOcrParser";
import type { AutoScanStats, ScanSummary } from "../../../lib/scannerSession";
import type { ScanActionContext } from "./scanViewScanActions";
import type {
  ReviewStateContext,
} from "./scanViewReviewHelpers";
import type { ArtifactRepositoryPort, ReviewSampleRepositoryPort, LearningRepositoryPort, AutomationRepositoryPort, RuntimeRepositoryPort } from "../../../infrastructure/repositories/rendererBridgeRepositoryTypes";
import type { BooleanResult, CaptureOptions, CaptureResult, ClickResult, ReviewSampleRecord, RuntimeInfo } from "../../../types/global";
import type { Dispatch, MutableRefObject, SetStateAction } from "react";

export interface ReviewStateContextInput {
  artifactRepo?: ArtifactRepositoryPort;
  reviewSamplesRepo?: ReviewSampleRepositoryPort;
  learningRepo?: LearningRepositoryPort;
  onStoredArtifactsChanged?: () => Promise<void> | void;
  setReviewSampleTotal: Dispatch<SetStateAction<number>>;
  setReviewSamples: Dispatch<SetStateAction<ReviewSampleRecord[]>>;
  setLearningRulesLoaded: Dispatch<SetStateAction<boolean>>;
  setScannerLearningRules: Dispatch<SetStateAction<ScannerLearningRules>>;
  setReviewStatus: Dispatch<SetStateAction<string>>;
  setStoredTotal: Dispatch<SetStateAction<number | null>>;
  appendAutomationLog: (line: string) => void;
}

export interface ScanActionContextInput {
  autoScanRunning: boolean;
  setAutoScanRunning: Dispatch<SetStateAction<boolean>>;
  isScanning: boolean;
  stopVisibleScanRef: MutableRefObject<boolean>;
  selectedSourceId: string;
  bridgeReady: boolean;
  automationRepo?: AutomationRepositoryPort;
  runtimeInfo?: RuntimeInfo | null;
  runtimeRepo?: RuntimeRepositoryPort;
  scanLimit: number;
  skipRows: number;
  detectedInventoryCount: number;
  setScanSummary: Dispatch<SetStateAction<ScanSummary | null>>;
  setAutoScanStats: Dispatch<SetStateAction<AutoScanStats>>;
  setReviewStatus: Dispatch<SetStateAction<string>>;
  appendAutomationLog: (line: string) => void;
  appendClickDiagnostics: (result: ClickResult, prefix?: string) => void;
  parseArtifact: (capture: CaptureResult | null) => ParsedArtifactCandidate | null;
  persistParsedArtifact: (
    capture: CaptureResult | null,
    parsed: ParsedArtifactCandidate,
    source: string,
    needsReview: boolean,
  ) => Promise<boolean>;
  saveReviewSample: (
    capture: CaptureResult | null,
    parsed: ParsedArtifactCandidate | null,
    reason?: string,
  ) => Promise<BooleanResult | null>;
  focusDashboard: () => Promise<void>;
  captureSelectedSource: (
    delayMs?: number,
    focusGenshin?: boolean,
    options?: CaptureOptions,
  ) => Promise<CaptureResult | null>;
  captureFastSelectedSource: (delayMs?: number, focusGenshin?: boolean) => Promise<CaptureResult | null>;
}

export function createReviewContext(input: ReviewStateContextInput): ReviewStateContext {
  return {
    artifactRepo: input.artifactRepo,
    reviewSamplesRepo: input.reviewSamplesRepo,
    learningRepo: input.learningRepo,
    onStoredArtifactsChanged: input.onStoredArtifactsChanged,
    setReviewSampleTotal: input.setReviewSampleTotal,
    setReviewSamples: input.setReviewSamples,
    setLearningRulesLoaded: input.setLearningRulesLoaded,
    setScannerLearningRules: input.setScannerLearningRules,
    setReviewStatus: input.setReviewStatus,
    setStoredTotal: input.setStoredTotal,
    appendAutomationLog: input.appendAutomationLog,
  };
}

export function createScanActionContext(input: ScanActionContextInput): ScanActionContext {
  return {
    autoScanRunning: input.autoScanRunning,
    setAutoScanRunning: input.setAutoScanRunning,
    isScanning: input.isScanning,
    stopVisibleScanRef: input.stopVisibleScanRef,
    selectedSourceId: input.selectedSourceId,
    bridgeReady: input.bridgeReady,
    automationRepo: input.automationRepo,
    runtimeInfo: input.runtimeInfo,
    runtimeRepo: input.runtimeRepo,
    scanLimit: input.scanLimit,
    skipRows: input.skipRows,
    detectedInventoryCount: input.detectedInventoryCount,
    setScanSummary: input.setScanSummary,
    setAutoScanStats: input.setAutoScanStats,
    setReviewStatus: input.setReviewStatus,
    appendAutomationLog: input.appendAutomationLog,
    appendClickDiagnostics: input.appendClickDiagnostics,
    parseArtifact: input.parseArtifact,
    persistParsedArtifact: input.persistParsedArtifact,
    shouldFlagArtifactForReview: (parsed) => (parsed ? shouldFlagArtifactForReview(parsed) : false),
    saveReviewSample: input.saveReviewSample,
    focusDashboard: input.focusDashboard,
    captureSelectedSource: input.captureSelectedSource,
    captureFastSelectedSource: input.captureFastSelectedSource,
  };
}
