import { registerAppHandlers } from "../ipc/appHandlers.js";
import { registerCaptureHandlers } from "../ipc/captureHandlers.js";
import { registerPersistenceHandlers } from "../ipc/persistenceHandlers.js";
import type {
  BooleanResult,
  FocusGenshinResult,
  RuntimeInfo,
  LoadScannerLearningRulesResult,
  SaveScannerLearningRulesResult,
  ScannerLearningRulePayload,
  CaptureOptions,
  CaptureResult,
  CaptureSourceInfo,
  ClickResult,
  AutomationGuard,
  ScrollResult,
  KeyPressResult,
  SaveResultWithPath,
  SaveSnapshotResult,
  GoodDatabase,
  GoodImportFileResult,
  NativeScannerCatalogStatus,
  NativeScannerDataStatus,
  NativeScannerImageLoadStatus,
  NativeScannerPreflightStatus,
  NativeScannerProcessStatus,
  NativeScannerPromotionStatus,
  NativeScannerReviewArtifactInput,
  NativeScannerReviewStatus,
  NativeScannerResultsLoadStatus,
  NativeScannerRunStatus,
  NativeScannerStartCategory,
  ScannerStatusPayload,
} from "../../src/types/global.js";
import type {
  ArtifactStoreRepositoryPort,
  ReviewSamplesRepositoryPort,
  ReviewSampleListResult,
} from "../repositories/contracts.js";
import type { AppSnapshot } from "../../src/types/domain.js";

interface AppHandlersDependencies {
  focusMainWindow: () => BooleanResult;
  moveMainWindowOffGenshin: () => Promise<void>;
  focusGenshinForScanStart: () => Promise<FocusGenshinResult>;
  publishScannerStatus: (status: ScannerStatusPayload) => Promise<BooleanResult>;
  nativeScannerDataStatus: () => Promise<NativeScannerDataStatus>;
  nativeScannerCatalog: () => Promise<NativeScannerCatalogStatus>;
  nativeScannerPreflight: (options?: { category?: NativeScannerStartCategory | string }) => Promise<NativeScannerPreflightStatus>;
  nativeScannerStart: (options?: { limit?: number; category?: NativeScannerStartCategory }) => Promise<NativeScannerRunStatus>;
  nativeScannerStop: () => Promise<NativeScannerRunStatus>;
  nativeScannerStatus: () => Promise<NativeScannerRunStatus>;
  nativeScannerProcessRun: (options?: { runDir?: string; persist?: boolean; limit?: number }) => Promise<NativeScannerProcessStatus>;
  nativeScannerLoadResults: (options?: { runDir?: string; limit?: number }) => Promise<NativeScannerResultsLoadStatus>;
  nativeScannerPromoteResults: (options: { runDir?: string; resultIds: string[] }) => Promise<NativeScannerPromotionStatus>;
  nativeScannerReviewResult: (options: { runDir?: string; resultId: string; action: "approve" | "reject"; artifact?: NativeScannerReviewArtifactInput; note?: string }) => Promise<NativeScannerReviewStatus>;
  nativeScannerLoadImage: (options: { runDir?: string; imagePath: string }) => Promise<NativeScannerImageLoadStatus>;
  readRuntimeInfo: () => Promise<RuntimeInfo>;
  loadSnapshotFromDisk: () => Promise<AppSnapshot | null>;
  saveSnapshotToDisk: (snapshot: AppSnapshot) => Promise<SaveSnapshotResult>;
  runMockScan: () => Promise<AppSnapshot | null>;
  showOverlayWindow: () => Promise<BooleanResult>;
  hideOverlayWindow: () => Promise<BooleanResult>;
}

type ArtifactStoreAccessor = () => ArtifactStoreRepositoryPort;
type ReviewSamplesAccessor = () => ReviewSamplesRepositoryPort;

interface PersistenceHandlersDependencies {
  getArtifactStoreRepository: ArtifactStoreAccessor;
  getReviewSamplesRepository: ReviewSamplesAccessor;
  artifactStorePath: () => string;
  reviewSamplesPath: () => string;
  loadReviewSamples: (limit?: number) => Promise<ReviewSampleListResult>;
  loadScannerLearningRules: () => Promise<LoadScannerLearningRulesResult>;
  writeScannerLearningRules: (rules: ScannerLearningRulePayload) => Promise<SaveScannerLearningRulesResult>;
  exportGood: (payload: GoodDatabase) => Promise<SaveResultWithPath>;
  importGoodFile: () => Promise<GoodImportFileResult>;
}

interface CaptureHandlersDependencies {
  listSources: () => Promise<CaptureSourceInfo[]>;
  captureSource: (sourceId: string, delayMs?: number, focusGenshin?: boolean, options?: CaptureOptions) => Promise<CaptureResult>;
  clickScreen: (x: number, y: number) => Promise<ClickResult>;
  scrollScreen: (notches: number, anchorX?: number, anchorY?: number) => Promise<ScrollResult>;
  keyPress: (key: string) => Promise<KeyPressResult>;
  getAutomationGuard: () => Promise<AutomationGuard>;
}

interface IpcBootstrapDependencies extends AppHandlersDependencies, PersistenceHandlersDependencies, CaptureHandlersDependencies {}

export function registerIpcHandlers(dependencies: IpcBootstrapDependencies) {
  registerAppHandlers({
    focusMainWindow: dependencies.focusMainWindow,
    moveMainWindowOffGenshin: dependencies.moveMainWindowOffGenshin,
    focusGenshinForScanStart: dependencies.focusGenshinForScanStart,
    publishScannerStatus: dependencies.publishScannerStatus,
    nativeScannerDataStatus: dependencies.nativeScannerDataStatus,
    nativeScannerCatalog: dependencies.nativeScannerCatalog,
    nativeScannerPreflight: dependencies.nativeScannerPreflight,
    nativeScannerStart: dependencies.nativeScannerStart,
    nativeScannerStop: dependencies.nativeScannerStop,
    nativeScannerStatus: dependencies.nativeScannerStatus,
    nativeScannerProcessRun: dependencies.nativeScannerProcessRun,
    nativeScannerLoadResults: dependencies.nativeScannerLoadResults,
    nativeScannerPromoteResults: dependencies.nativeScannerPromoteResults,
    nativeScannerReviewResult: dependencies.nativeScannerReviewResult,
    nativeScannerLoadImage: dependencies.nativeScannerLoadImage,
    getRuntimeInfo: dependencies.readRuntimeInfo,
    loadSnapshot: dependencies.loadSnapshotFromDisk,
    saveSnapshot: dependencies.saveSnapshotToDisk,
    runMockScan: dependencies.runMockScan,
    showOverlay: dependencies.showOverlayWindow,
    hideOverlay: dependencies.hideOverlayWindow,
  });

  registerPersistenceHandlers({
    getArtifactStoreRepository: dependencies.getArtifactStoreRepository,
    getReviewSamplesRepository: dependencies.getReviewSamplesRepository,
    artifactStorePath: dependencies.artifactStorePath,
    reviewSamplesPath: dependencies.reviewSamplesPath,
    loadReviewSamples: dependencies.loadReviewSamples,
    loadScannerLearningRules: dependencies.loadScannerLearningRules,
    writeScannerLearningRules: dependencies.writeScannerLearningRules,
    exportGood: dependencies.exportGood,
    importGoodFile: dependencies.importGoodFile,
  });

  registerCaptureHandlers({
    listSources: dependencies.listSources,
    captureSource: dependencies.captureSource,
    clickScreen: dependencies.clickScreen,
    scrollScreen: dependencies.scrollScreen,
    keyPress: dependencies.keyPress,
    getAutomationGuard: dependencies.getAutomationGuard,
  });
}
