export type DbFlavor = "postgresql" | "greenplum" | "clickhouse";

export interface ConnectResult {
  version: string;
  flavor: DbFlavor;
}

export interface ConnectionConfig {
  id: string;
  name: string;
  host: string;
  port: number;
  user: string;
  password: string;
  database: string;
  ssl_mode?: string;
  color?: string;
  environment?: string;
  /** DB engine selected by user. Older configs without this default to "postgresql". */
  db_flavor?: DbFlavor;
  /** HTTPS for ClickHouse. Defaults to false. */
  secure?: boolean;
}

export interface QueryResult {
  columns: string[];
  types: string[];
  rows: unknown[][];
  row_count: number;
  execution_time_ms: number;
}

export interface PaginatedQueryResult extends QueryResult {
  total_rows: number;
  page: number;
  page_size: number;
  ctids: string[];
}

export interface SchemaObject {
  name: string;
  object_type: string;
  schema: string;
  row_count?: number;
  size_bytes?: number;
}

export interface ColumnDetail {
  column_name: string;
  data_type: string;
  is_nullable: boolean;
  column_default: string | null;
  is_identity: boolean;
}

export interface ColumnInfo {
  name: string;
  data_type: string;
  is_nullable: boolean;
  column_default: string | null;
  ordinal_position: number;
  character_maximum_length: number | null;
  is_primary_key: boolean;
  comment: string | null;
}

export interface ConstraintInfo {
  name: string;
  constraint_type: string;
  columns: string[];
  referenced_schema: string | null;
  referenced_table: string | null;
  referenced_columns: string[] | null;
  update_rule: string | null;
  delete_rule: string | null;
}

export interface IndexInfo {
  name: string;
  definition: string;
  is_unique: boolean;
  is_primary: boolean;
}

export interface HistoryEntry {
  id: string;
  connection_id: string;
  connection_name: string;
  database: string;
  sql: string;
  status: string;
  error_message?: string;
  row_count?: number;
  execution_time_ms: number;
  executed_at: string;
}

export interface ExplainNode {
  "Node Type": string;
  "Relation Name"?: string;
  "Schema"?: string;
  "Alias"?: string;
  "Startup Cost": number;
  "Total Cost": number;
  "Plan Rows": number;
  "Plan Width": number;
  "Actual Startup Time"?: number;
  "Actual Total Time"?: number;
  "Actual Rows"?: number;
  "Actual Loops"?: number;
  "Shared Hit Blocks"?: number;
  "Shared Read Blocks"?: number;
  "Filter"?: string;
  "Join Type"?: string;
  "Index Name"?: string;
  "Index Cond"?: string;
  "Hash Cond"?: string;
  "Sort Key"?: string[];
  Plans?: ExplainNode[];
  [key: string]: unknown;
}

export interface ExplainResult {
  Plan: ExplainNode;
  "Planning Time": number;
  "Execution Time": number;
}

export interface SavedQuery {
  id: string;
  name: string;
  sql: string;
  connection_id?: string;
  created_at: string;
}

export type AiProvider = "ollama" | "fireworks" | "openrouter";

export interface AiSettings {
  provider: AiProvider;
  ollama_url: string;
  fireworks_api_key?: string;
  openrouter_api_key?: string;
  model: string;
}

export interface OllamaModel {
  name: string;
}

export interface TriggerInfo {
  name: string;
  event: string;
  timing: string;
  orientation: string;
  function_name: string;
  is_enabled: boolean;
  definition: string;
}

// App Settings
export interface McpSettings {
  enabled: boolean;
  port: number;
}

export interface AppSettings {
  mcp: McpSettings;
}

export interface McpStatus {
  enabled: boolean;
  port: number;
  running: boolean;
}

export type TabType = "query" | "table" | "structure" | "chat";

export interface Tab {
  id: string;
  type: TabType;
  title: string;
  connectionId: string;
  database?: string;
  schema?: string;
  table?: string;
  sql?: string;
}

export type ChatRole = "user" | "assistant" | "tool_call" | "tool_result";

interface ChatBase {
  id: string;
  created_at: number;
}

export type ChatMessage =
  | (ChatBase & { role: "user"; text: string })
  | (ChatBase & { role: "assistant"; text: string })
  | (ChatBase & { role: "tool_call"; tool: string; input_json: string })
  | (ChatBase & {
      role: "tool_result";
      tool: string;
      is_error: boolean;
      text?: string | null;
      result?: QueryResult | null;
    });

export interface ContextUsage {
  used_chars: number;
  budget_chars: number;
}

export interface ChatTurnResult {
  messages: ChatMessage[];
  usage: ContextUsage;
}
