/**
 * TypeScript types for the pipeline system.
 *
 * These types mirror the Python contracts in pipeline_core/contracts.py
 */

// Widget types supported by the dashboard
export type WidgetType =
  | 'stat_card'
  | 'line_chart'
  | 'bar_chart'
  | 'pie_chart'
  | 'table'
  | 'heatmap'
  | 'area_chart'
  | 'gauge';

// Grid position for dashboard layout
export interface GridPosition {
  x: number;
  y: number;
  w: number;
  h: number;
}

// Widget configuration
export interface WidgetConfig {
  id: string;
  type: WidgetType;
  title: string;
  grid: GridPosition;
  config: Record<string, unknown>;
  data_endpoint?: string;
  refresh_interval?: number;
}

// Dashboard section containing widgets
export interface DashboardSection {
  id: string;
  title: string;
  description?: string;
  widgets: WidgetConfig[];
  collapsed?: boolean;
}

// Full dashboard configuration
export interface DashboardConfig {
  pipeline_id: string;
  title: string;
  description?: string;
  sections: DashboardSection[];
  default_time_range?: string;
  refresh_interval?: number;
}

// Pipeline info (summary)
export interface PipelineInfo {
  id: string;
  name: string;
  description: string;
  version: string;
  is_enabled: boolean;
  stages: string[];
  input_type: string;
}

// Pipeline detail (full info)
export interface PipelineDetail extends PipelineInfo {
  module_path: string;
  config?: Record<string, unknown>;
  created_at?: string;
  updated_at?: string;
}

// Per-stage execution metrics
export interface StageMetrics {
  duration_ms: number;
  success: boolean;
  records_in: number;
  records_out: number;
  error?: string;
}

// Execution status
export interface ExecutionStatus {
  id: string;
  pipeline_id: string;
  job_id?: string;
  business_id?: string;
  status: 'pending' | 'running' | 'completed' | 'failed' | 'cancelled';
  stages_requested: string[];
  stages_completed: string[];
  current_stage?: string;
  progress: number;
  error_message?: string;
  started_at?: string;
  completed_at?: string;
  created_at?: string;
  total_duration_ms?: number;
  input_summary?: Record<string, unknown>;
  result_summary?: Record<string, unknown>;
  stage_metrics?: Record<string, StageMetrics>;
}

// Widget-specific data types

export interface StatCardData {
  [key: string]: number | string;
}

export interface ChartDataPoint {
  [key: string]: number | string;
}

export interface ChartData {
  data: ChartDataPoint[];
}

export interface TableData {
  data: Record<string, unknown>[];
  total: number;
}

export type WidgetData = StatCardData | ChartData | TableData;

// Widget props base
export interface WidgetProps {
  config: WidgetConfig;
  data: WidgetData | null;
  loading: boolean;
  error?: string;
  onRefresh?: () => void;
}

// Stat card specific config
export interface StatCardConfig {
  value_key: string;
  label?: string;
  format?: string;
  trend_key?: string;
  trend_format?: string;
  icon?: string;
  color?: string;
}

// Chart config
export interface ChartAxisConfig {
  key: string;
  label?: string;
  type?: 'number' | 'category' | 'time';
  format?: string;
}

export interface ChartSeriesConfig {
  key: string;
  name: string;
  color?: string;
  type?: 'line' | 'bar' | 'area';
}

export interface ChartWidgetConfig {
  x_axis: ChartAxisConfig;
  y_axis: ChartAxisConfig;
  series: ChartSeriesConfig[];
  stacked?: boolean;
  show_legend?: boolean;
  show_grid?: boolean;
}

// Pie chart config
export interface PieChartConfig {
  value_key: string;
  label_key: string;
  colors?: string[];
  show_legend?: boolean;
  show_labels?: boolean;
  inner_radius?: number;
}

// Table config
export interface TableColumnConfig {
  key: string;
  header: string;
  width?: number;
  align?: 'left' | 'center' | 'right';
  format?: string;
  sortable?: boolean;
}

export interface TableWidgetConfig {
  columns: TableColumnConfig[];
  row_key: string;
  page_size?: number;
  show_pagination?: boolean;
  sortable?: boolean;
  filterable?: boolean;
}

// Heatmap config
export interface HeatmapConfig {
  x_key: string;
  y_key: string;
  value_key: string;
  color_scale?: string[];
  show_values?: boolean;
  format?: string;
}
