export interface Conversation {
  id: string
  title: string
  created_at: string
  updated_at: string
  messages?: Message[]
}

export interface Message {
  id: string
  conversation_id: string
  role: 'user' | 'assistant' | 'system'
  content: string
  sql_query?: string
  has_results: boolean
  result_table?: string
  result_csv_path?: string
  row_count?: number
  chart_config?: ChartConfig
  execution_time_ms?: number
  model_used?: string
  tokens_used?: number
  created_at: string
}

export interface ChartConfig {
  chart_type: 'bar' | 'line' | 'pie' | 'scatter'
  x_column: string
  y_column: string
  title: string
}

export interface QueryResult {
  columns: string[]
  rows: Record<string, any>[]
  row_count: number
}

export interface DatabaseConnection {
  id: string
  name: string
  db_type: string
  source_system?: string
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface TableInfo {
  id: string
  connection_id?: string
  connection_name?: string
  source_schema?: string
  source_table: string
  local_table?: string
  display_name?: string
  business_description?: string
  source_system?: string
  row_count?: number
  is_synced: boolean
  sync_schedule?: string
  columns?: ColumnInfo[]
}

export interface ColumnInfo {
  id: string
  table_id: string
  column_name: string
  data_type: string
  nullable: boolean
  is_primary_key: boolean
  max_length?: number
  display_name?: string
  business_description?: string
}

export interface JoinDefinition {
  id: string
  left_table_id: string
  left_column: string
  left_table?: string
  right_table_id: string
  right_column: string
  right_table?: string
  join_type: string
  confidence?: number
  description?: string
}

export interface SetupStatus {
  is_configured: boolean
  has_connections: boolean
  has_tables: boolean
  has_api_key: boolean
  ollama_available: boolean
  connection_count: number
  table_count: number
}

export interface StreamEvent {
  type: 'stream_start' | 'stream_chunk' | 'status' | 'sql' | 'results' | 'stream_end' | 'error' | 'pong' | 'confirm_api' | 'confirm_sql'
  conversation_id?: string
  content?: string
  message?: string
  query?: string
  message_id?: string
  row_count?: number
  columns?: string[]
  chart_config?: ChartConfig
  csv_path?: string
  // Confirmation fields
  description?: string
  metadata_preview?: string
  tables?: string[]
  attempt?: number
}

export interface DocumentChain {
  id: string
  chain_reference: string
  description?: string
  documents: DocumentLink[]
  created_at: string
  updated_at: string
}

export interface DocumentLink {
  id: string
  chain_id: string
  source_system: string
  entity_name: string
  doc_type: string
  doc_number: string
  doc_date?: string
  reference_number?: string
  customer_vendor?: string
  total_amount?: number
  currency?: string
  status?: string
}

export interface DiscoveredConnection {
  name: string
  source: string  // 'ODBC DSN' | 'DBeaver' | 'SSMS' | 'SQL Server Browser'
  db_type: string
  source_system: string
  driver: string
  server: string
  port: string
  database: string
  connection_string_template: string
  needs_credentials: boolean
  version?: string
}

export interface DbPreset {
  key: string
  label: string
  default_port: number
  drivers: string[]
  auth_modes: string[]
  placeholder: Record<string, string>
  notes: string
}

export interface AuditEntry {
  id: string
  event_type: string
  direction: string
  payload_hash: string
  payload_size: number
  contains_data: boolean
  metadata?: Record<string, any>
  created_at: string
}

export interface Subscription {
  id: string
  email: string
  doc_number: string
  chain_id?: string
  source_system?: string
  doc_type?: string
  notify_on_stages?: string[]
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface NotificationEntry {
  id: string
  subscription_id?: string
  event_id?: string
  email: string
  doc_number: string
  subject: string
  status: 'pending' | 'sent' | 'failed'
  error_message?: string
  retry_count: number
  created_at: string
  sent_at?: string
}

export interface StatusStage {
  id: string
  doc_type: string
  source_system: string
  stage_order: number
  stage_key: string
  stage_label: string
  created_at: string
}

export interface SyncEvent {
  id: string
  table_id: string
  local_table: string
  row_key: Record<string, any>
  column_name: string
  old_value?: string
  new_value?: string
  change_type: 'changed' | 'added' | 'removed'
  chain_id?: string
  doc_number?: string
  processed: boolean
  detected_at: string
}

export interface WatchColumn {
  id: string
  table_id: string
  column_name: string
  is_status_column: boolean
  is_key_column: boolean
  is_doc_number_column: boolean
  created_at: string
}

export interface SyncJob {
  id: string
  connection_id?: string
  connection_name: string
  db_type: string
  source_schema: string
  source_table: string
  local_table?: string
  status: 'pending' | 'running' | 'completed' | 'failed' | 'cancelled'
  phase?: string
  phase_detail?: string
  rows_fetched: number
  rows_inserted: number
  total_columns?: number
  batches_done: number
  fetch_rate?: number
  insert_rate?: number
  error_message?: string
  started_at?: string
  finished_at?: string
  created_at: string
  updated_at: string
}
