/**
 * Editor type definitions
 * @countermeasure/web-components/editors
 */

export type EditorTheme = 'light' | 'dark' | 'auto'

export interface BaseEditorConfig {
  /** Container element or CSS selector */
  container: HTMLElement | string
  /** Initial content */
  value?: string
  /** Read-only mode */
  readOnly?: boolean
  /** Show line numbers (default: true) */
  lineNumbers?: boolean
  /** Enable word wrap (default: false) */
  wordWrap?: boolean
  /** Theme override ('auto' follows page theme) */
  theme?: EditorTheme
  /** Placeholder text when empty */
  placeholder?: string
  /** Tab size in spaces (default: 2) */
  tabSize?: number
  /** Called when content changes (debounced internally) */
  onChange?: (value: string) => void
}

export interface CodeEditorConfig extends BaseEditorConfig {
  /** Language identifier (fluxl, sql, yaral, yara, json, yaml) */
  language?: string
  /** Schema for autocomplete suggestions */
  autocomplete?: AutocompleteSchema
  /** Live validation endpoint configuration */
  validation?: ValidationConfig
  /** Custom keybindings (key combo → handler) */
  keybindings?: Record<string, () => void>
}

export interface AutocompleteSchema {
  /** Table schemas for table/column completion */
  tables?: TableSchema[]
  /** Additional function names to suggest */
  functions?: string[]
  /** Additional operator names to suggest */
  operators?: string[]
  /** Arbitrary custom completion items */
  custom?: CompletionItem[]
}

export interface TableSchema {
  name: string
  description?: string
  columns: ColumnSchema[]
}

export interface ColumnSchema {
  name: string
  dataType: string
  nullable?: boolean
}

export interface CompletionItem {
  label: string
  type: 'keyword' | 'function' | 'class' | 'property' | 'operator' | 'variable'
  info?: string
  detail?: string
}

export interface ValidationConfig {
  /** URL to POST validation requests to */
  endpoint: string
  /** Debounce interval in ms (default: 800) */
  debounceMs?: number
  /** HTTP method (default: POST) */
  method?: 'POST' | 'GET'
  /** Additional request headers */
  headers?: Record<string, string>
  /** Transform the request body before sending */
  transformRequest?: (query: string, language: string) => unknown
  /** Transform the response into a ValidationResult */
  transformResponse?: (data: unknown) => ValidationResult
}

export interface ValidationResult {
  valid: boolean
  errors?: ValidationError[]
  warnings?: string[]
}

export interface ValidationError {
  message: string
  line?: number
  column?: number
  severity?: 'error' | 'warning' | 'info'
}

export interface LogViewerConfig extends BaseEditorConfig {
  /** Maximum number of lines to retain (default: 10000) */
  maxLines?: number
  /** Auto-scroll to bottom on new content (default: true) */
  followMode?: boolean
  /** Enable severity-based line coloring (default: true) */
  severityColors?: boolean
}

export type LogSeverity = 'info' | 'warning' | 'error' | 'success'

export interface LogLine {
  text: string
  severity?: LogSeverity
  timestamp?: string
}

export interface JsonViewerConfig extends BaseEditorConfig {
  /** Pretty-print JSON on load (default: true) */
  formatOnLoad?: boolean
  /** Allow editing (default: false, view-only) */
  editable?: boolean
}

export interface YamlEditorConfig extends BaseEditorConfig {
  /** Indent size for YAML (default: 2) */
  indentSize?: number
}

export interface DiffViewerConfig {
  /** Container element or CSS selector */
  container: HTMLElement | string
  /** Original (left) content */
  original: string
  /** Modified (right) content */
  modified: string
  /** Theme override */
  theme?: EditorTheme
  /** Show line numbers (default: true) */
  lineNumbers?: boolean
}
