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,
  SaveResultWithPath,
  SaveSnapshotResult,
  GoodDatabase,
  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>;
  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>;
}

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>;
  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,
    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,
  });

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