import type {
  AutomationGuard,
  CaptureOptions,
  CaptureResult,
  CaptureSourceInfo,
  ClickResult,
  ReviewSampleListResult,
  ScannerStatusPayload,
  ReviewSamplePayload,
  GoodDatabase,
  FocusGenshinResult,
  RuntimeInfo,
  LoadScannerLearningRulesResult,
  SaveScannerLearningRulesResult,
  ArtifactStoreLoadResult,
  ArtifactStoreSaveResult,
  BooleanResult,
  SaveResultWithPath,
  ScrollResult,
  ScannerLearningRulePayload,
} from "../../types/global";
import type { AppSnapshot } from "../../types/domain";
import type { StoredArtifactRecord } from "../../types/storage";

export interface ArtifactRepositoryPort {
  loadAll(): Promise<ArtifactStoreLoadResult>;
  saveMany(records: StoredArtifactRecord[]): Promise<ArtifactStoreSaveResult>;
}

export interface CaptureRepositoryPort {
  listSources(): Promise<CaptureSourceInfo[]>;
  captureSource(sourceId: string, delayMs?: number, focusGenshin?: boolean, options?: CaptureOptions): Promise<CaptureResult>;
}

export interface RuntimeRepositoryPort {
  getRuntimeInfo(): Promise<RuntimeInfo>;
}

export interface ReviewSampleRepositoryPort {
  loadSamples(limit?: number): Promise<ReviewSampleListResult>;
  saveSample(sample: ReviewSamplePayload): Promise<SaveResultWithPath>;
}

export interface LearningRepositoryPort {
  loadRules(): Promise<LoadScannerLearningRulesResult>;
  saveRules(
    rules: ScannerLearningRulePayload,
  ): Promise<SaveScannerLearningRulesResult>;
}

export interface SnapshotRepositoryPort {
  load(): Promise<AppSnapshot | null>;
  save(snapshot: AppSnapshot): Promise<SaveResultWithPath>;
  runMockScan(): Promise<AppSnapshot | null>;
  publishScannerStatus(status: ScannerStatusPayload): Promise<BooleanResult>;
}

export interface AutomationRepositoryPort {
  getAutomationGuard(): Promise<AutomationGuard>;
  focusGenshin(): Promise<FocusGenshinResult>;
  focusMainWindow(): Promise<BooleanResult>;
  clickScreen(x: number, y: number): Promise<ClickResult>;
  scrollScreen(notches: number, anchorX?: number, anchorY?: number): Promise<ScrollResult>;
  onCommand(callback: (command: "start-auto" | "stop") => void): () => void;
}

export interface OverlayRepositoryPort {
  show(): Promise<BooleanResult>;
}

export interface ScanExportPort {
  exportGood(payload: GoodDatabase): Promise<SaveResultWithPath>;
}

export interface RendererRepositories {
  artifacts: ArtifactRepositoryPort;
  capture: CaptureRepositoryPort;
  runtime: RuntimeRepositoryPort;
  reviewSamples: ReviewSampleRepositoryPort;
  learning: LearningRepositoryPort;
  snapshot: SnapshotRepositoryPort;
  automation: AutomationRepositoryPort;
  overlay: OverlayRepositoryPort;
  export: ScanExportPort;
  canExportGood: boolean;
  canShowOverlay: boolean;
  canAutoScan: boolean;
  canReviewSamples: boolean;
  isAvailable: boolean;
}
