export interface ApiErrorResponse {
  error: string;
  status: number;
}

export interface CheckFailure {
  actual?: unknown;
  expected?: unknown;
  kind: "error" | "mismatch";
  message: string;
  path: string;
  phase: string;
}

export interface CheckResult {
  detail: null | string;
  durationMs: null | number;
  failure: CheckFailure | null;
  matched: boolean;
  observation: null | Record<string, unknown>;
  timestamp: string;
}

export interface CurrentStreak {
  capped?: boolean;
  count: number;
  up: boolean;
}

export interface DashboardResponse {
  summary: {
    down: number;
    incidents: number;
    lastCheckTime: null | string;
    total: number;
    up: number;
    window: {
      from: string;
      label: string;
      to: string;
    };
  };
  targets: TargetStatus[];
}

export interface HealthResponse {
  ok: true;
  service: "dial-server";
  timestamp: string;
}

export interface HistoryResponse {
  items: CheckResult[];
  page: number;
  pageSize: number;
  total: number;
}

export interface MetaResponse {
  checkerTypes: string[];
  version: string;
}

export interface RecentSample {
  durationMs: null | number;
  timestamp: string;
  up: boolean;
}

export type RuntimeMode = "development" | "production" | "test";

export interface TargetMetricsResponse {
  stats: {
    availability: number;
    avgDurationMs: null | number;
    currentStreak: CurrentStreak | null;
    downChecks: number;
    incidentCount: number;
    longestOutage: null | number;
    mttr: null | number;
    p95DurationMs: null | number;
    p99DurationMs: null | number;
    totalChecks: number;
    upChecks: number;
  };
  targetId: string;
  trend: TrendPoint[];
  window: {
    bucket: "1h";
    from: string;
    to: string;
  };
}

export interface TargetStats {
  availability: number;
  downChecks: number;
  totalChecks: number;
  upChecks: number;
}

export interface TargetStatus {
  currentStreak: CurrentStreak | null;
  description: null | string;
  group: string;
  id: string;
  interval: string;
  latestCheck: CheckResult | null;
  name: null | string;
  recentSamples: RecentSample[];
  stats: TargetStats;
  target: string;
  type: string;
}

export interface TrendPoint {
  availability: number;
  avgDurationMs: null | number;
  bucketStart: string;
  downChecks: number;
  maxDurationMs: null | number;
  minDurationMs: null | number;
  totalChecks: number;
  upChecks: number;
}
