import type { CategoryInfo } from './category'

// Tree node types
export type NodeType = 'decision' | 'action' | 'solution' | 'answer'

export interface TreeOption {
  id: string
  label: string
  next_node_id: string
}

export interface TreeNodeBase {
  id: string
  type: NodeType
}

export interface DecisionNode extends TreeNodeBase {
  type: 'decision'
  question: string
  help_text?: string
  options: TreeOption[]
  children: TreeNode[]
}

export interface ActionNode extends TreeNodeBase {
  type: 'action'
  title: string
  description: string
  commands?: string[]
  expected_outcome?: string
  next_node_id?: string
  children?: TreeNode[]
}

export interface SolutionNode extends TreeNodeBase {
  type: 'solution'
  title: string
  description: string
  resolution_steps?: string[]
}

export type TreeNode = DecisionNode | ActionNode | SolutionNode

export interface TreeStructure {
  id: string
  type: NodeType
  question?: string
  title?: string
  description?: string
  help_text?: string
  options?: TreeOption[]
  commands?: string[]
  expected_outcome?: string
  next_node_id?: string
  resolution_steps?: string[]
  children?: TreeStructure[]
}

// --- Procedural Flow Types ---

export type TreeType = 'troubleshooting' | 'procedural' | 'maintenance'

export type IntakeFieldType =
  | 'text' | 'textarea' | 'number' | 'ip_address' | 'email' | 'url'
  | 'select' | 'multi_select' | 'checkbox' | 'password'

export type StepContentType = 'action' | 'informational' | 'verification' | 'warning'

export interface IntakeFieldValidation {
  min_length?: number
  max_length?: number
  pattern?: string
  pattern_message?: string
  format?: 'ipv4' | 'email'
  min_value?: number
  max_value?: number
  min_selections?: number
  max_selections?: number
}

export interface IntakeFormField {
  variable_name: string
  label: string
  field_type: IntakeFieldType
  required: boolean
  options?: string[]
  placeholder?: string
  help_text?: string
  default_value?: string
  group_name?: string
  display_order: number
  validation?: IntakeFieldValidation
}

export interface CommandBlock {
  language?: string
  code: string
  label?: string
}

export interface StepVerification {
  type: 'checkbox' | 'text_input'
  prompt: string
}

export interface ProceduralStep {
  id: string
  type: 'procedure_step' | 'procedure_end' | 'section_header'
  title: string
  description?: string
  content_type?: StepContentType
  estimated_minutes?: number
  warning_text?: string
  // Verification — supports both flat fields and nested object
  verification_prompt?: string
  verification_type?: 'checkbox' | 'text_input'
  verification?: StepVerification
  // Commands — supports both string and array of command blocks
  commands?: string | CommandBlock[]
  expected_outcome?: string
  notes_enabled?: boolean
  section_header?: string
  reference_url?: string
  library_visibility?: 'team' | 'public'
}

export interface CustomProceduralStep {
  id: string
  type: 'procedure_step'
  title: string
  description?: string
  content_type: 'action'
  commands?: CommandBlock[]
  isCustom: true
}

export type RuntimeStep = ProceduralStep | CustomProceduralStep

export interface ProceduralTreeStructure {
  steps: ProceduralStep[]
}

// API response types
export type TreeStatus = 'draft' | 'published'

/** Fork lineage metadata. Only present (non-null) on trees that are forks (fork_depth > 0). */
export interface ForkInfo {
  parent_tree_id: string | null
  root_tree_id: string | null
  fork_reason: string | null
  fork_depth: number
  parent_updated_at: string | null
  has_parent_updates: boolean
}

export interface Tree {
  id: string
  name: string
  description: string | null
  tree_type: TreeType
  category: string | null
  category_id: string | null
  category_info: CategoryInfo | null
  tags: string[]
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  tree_structure: TreeStructure & Record<string, any>
  intake_form: IntakeFormField[] | null
  author_id: string | null
  account_id: string | null
  is_active: boolean
  is_public: boolean
  is_default: boolean
  status: TreeStatus
  version: number
  created_at: string
  updated_at: string
  usage_count: number
  fork_info: ForkInfo | null
}

export interface TreeListItem {
  id: string
  name: string
  description: string | null
  tree_type: TreeType
  category: string | null
  category_id: string | null
  category_info: CategoryInfo | null
  tags: string[]
  author_id: string | null
  author_name: string | null
  account_id: string | null
  is_active: boolean
  is_public: boolean
  is_default: boolean
  status: TreeStatus
  version: number
  usage_count: number
  created_at: string
  updated_at: string
  visibility: 'private' | 'team' | 'link' | 'public'
}

export interface TreeCreate {
  name: string
  description?: string
  tree_type?: TreeType
  category?: string
  category_id?: string | null
  tags?: string[]
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  tree_structure: Record<string, any>
  intake_form?: IntakeFormField[]
  is_public?: boolean
  is_default?: boolean
  status?: TreeStatus
}

export interface TreeUpdate {
  name?: string
  description?: string
  tree_type?: TreeType
  category?: string
  category_id?: string | null
  tags?: string[]
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  tree_structure?: Record<string, any>
  intake_form?: IntakeFormField[]
  is_active?: boolean
  is_public?: boolean
  status?: TreeStatus
}

// Filter params for tree listing
export interface TreeFilters {
  tree_type?: TreeType
  category?: string
  category_id?: string
  tags?: string
  folder_id?: string
  is_active?: boolean
  author_id?: string
  is_public?: boolean
  visibility?: 'private' | 'team' | 'link' | 'public'
  sort_by?: 'usage_count' | 'updated_at' | 'created_at' | 'name' | 'name_desc' | 'version'
  skip?: number
  limit?: number
}

// Tree sharing types
export type TreeVisibility = 'private' | 'team' | 'link' | 'public'

export interface TreeShareCreate {
  allow_forking?: boolean
  expires_at?: string | null
}

export interface TreeShare {
  id: string
  tree_id: string
  share_token: string
  share_url: string
  allow_forking: boolean
  created_by: string
  created_at: string
  expires_at: string | null
}

export interface TreeVisibilityUpdate {
  visibility: TreeVisibility
}

export interface SharedTree {
  id: string
  name: string
  description: string | null
  category: string | null
  tree_structure: TreeStructure
  tags: string[]
  version: number
  allow_forking: boolean
  created_at: string
  updated_at: string
}

// Tree validation types
export interface ValidationError {
  field: string
  message: string
}

export interface TreeValidationResponse {
  can_publish: boolean
  errors: ValidationError[]
}

// Tree markdown types
export interface TreeMarkdownValidationError {
  line: number
  column: number
  message: string
  severity: 'error' | 'warning'
}

export interface TreeMarkdownMetadata {
  name?: string
  description?: string
  category?: string
  tags?: string[]
}

export interface TreeMarkdownValidation {
  valid: boolean
  errors: TreeMarkdownValidationError[]
  tree_structure: TreeStructure | null
  metadata?: TreeMarkdownMetadata | null
}
