import type { PublishPlatform, QaReport } from "../domain/types";

export type CalibrationDirection =
  | "better_than_expected"
  | "as_expected"
  | "worse_than_expected"
  | "needs_more_data";

export type PerformanceSource = "manual" | `adapter:${string}`;

export interface RubricDimension {
  id: string;
  label: string;
  weight: number;
  description: string;
}

export interface RubricVersion {
  id: string;
  version: string;
  name: string;
  dimensions: RubricDimension[];
  formula: "weighted_average_0_to_10";
  is_active: boolean;
  created_at: string;
}

export interface ScoringRun {
  id: string;
  job_id: string;
  revision: number;
  rubric_version_id: string;
  dimension_scores: Record<string, number>;
  composite_score: number;
  rationale: string;
  created_at: string;
}

export interface PublicationRecord {
  id: string;
  job_id: string;
  revision: number;
  platform: PublishPlatform;
  url: string;
  published_at: string;
  status: "draft" | "published" | "archived";
  notes: string;
  created_at: string;
  updated_at: string;
}

export interface PerformanceMetrics {
  views?: number;
  impressions?: number;
  clicks?: number;
  inquiries?: number;
  likes?: number;
  comments?: number;
  shares?: number;
  saves?: number;
  average_position?: number;
}

export interface PerformanceSnapshot {
  id: string;
  publication_id: string;
  source: PerformanceSource;
  window_label: string;
  metrics: PerformanceMetrics;
  feedback_summary: string;
  raw_reference?: string;
  snapshot_at: string;
}

export interface CalibrationEvent {
  id: string;
  publication_id: string;
  scoring_run_id: string;
  performance_snapshot_id: string;
  direction: CalibrationDirection;
  observations: string[];
  recommended_action: string;
  created_at: string;
}

export interface AdapterFetchInput {
  publication: PublicationRecord;
  window_label: string;
}

export interface PerformanceAdapter {
  source: PerformanceSource;
  fetch(input: AdapterFetchInput): Promise<PerformanceSnapshot>;
}

export interface CalibrationContext {
  scoringRun: ScoringRun;
  qaReport: QaReport;
  snapshot: PerformanceSnapshot;
}
