import type { ActionType, ConditionField, ConditionOperator, WorkflowNodeType } from './types'
import {
  ACTION_TYPE_META,
  CONDITION_FIELD_LABELS,
  TRIGGER_LABELS,
  actionTypesForDomains,
  conditionFieldsForDomains,
  inferDomainsFromTriggers,
  primaryDomainFromTriggers,
} from './domains'
import type { AutomationDomain } from './domains'
import type { TriggerOrGroup } from './types'
import {
  LABEL_CONDITION_OPERATORS,
  STRING_CONDITION_OPERATORS,
  operatorOptionsForConditionField,
} from './condition-helpers'

export { LABEL_CONDITION_OPERATORS, STRING_CONDITION_OPERATORS, operatorOptionsForConditionField }
export { TRIGGER_LABELS, CONDITION_FIELD_LABELS, ACTION_TYPE_META }

export const NODE_TYPE_LABELS: Record<WorkflowNodeType, string> = {
  start: 'Début',
  condition: 'Condition',
  label_check: 'Vérifier libellé',
  switch: 'Switch',
  llm_check: 'Vérification LLM',
  actions: 'Actions',
  set_var: 'Variable',
  call_function: 'Appeler fonction',
  call_rule: 'Appeler règle',
  end: 'Fin',
}

export const NODE_TYPE_DESCRIPTIONS: Record<WorkflowNodeType, string> = {
  start: "Point d'entrée du flux",
  condition: 'If / else sur métadonnées de l’événement déclencheur',
  label_check: 'Ancien nœud libellé (legacy)',
  switch: 'Branchement multi-cas',
  llm_check: 'Décision via prompt LLM',
  actions: 'Exécute une ou plusieurs actions',
  set_var: "Définit une variable d'exécution",
  call_function: 'Invoque une règle-fonction réutilisable',
  call_rule: 'Enchaîne une autre règle (cascade)',
  end: 'Termine le flux',
}

/** @deprecated Préférer conditionFieldsForDomains(activeDomains) */
export const CONDITION_FIELDS: { value: ConditionField; label: string }[] = conditionFieldsForDomains([
  'mail',
  'drive',
  'contacts',
]).map(({ value, label }) => ({ value, label }))

export const CONDITION_OPERATORS = STRING_CONDITION_OPERATORS

/** @deprecated Préférer actionTypesForDomains(activeDomains) */
export const ACTION_TYPES: {
  value: ActionType
  label: string
  needsValue: boolean
  placeholder?: string
}[] = actionTypesForDomains(['mail', 'drive', 'contacts']).map(
  ({ value, label, needsValue, placeholder }) => ({ value, label, needsValue, placeholder })
)

export function conditionFieldsForWorkflow(triggers: TriggerOrGroup) {
  return conditionFieldsForDomains(inferDomainsFromTriggers(triggers))
}

export function actionTypesForWorkflow(triggers: TriggerOrGroup) {
  return actionTypesForDomains(inferDomainsFromTriggers(triggers))
}

export function primaryDomainForWorkflow(triggers: TriggerOrGroup): AutomationDomain {
  return primaryDomainFromTriggers(triggers)
}

export const PALETTE_NODE_TYPES: WorkflowNodeType[] = [
  'condition',
  'switch',
  'llm_check',
  'actions',
  'set_var',
  'call_function',
  'call_rule',
]

export const NODE_COLORS: Record<WorkflowNodeType, string> = {
  start: 'border-emerald-500/60 bg-emerald-500/10',
  condition: 'border-blue-500/60 bg-blue-500/10',
  label_check: 'border-indigo-500/60 bg-indigo-500/10',
  switch: 'border-violet-500/60 bg-violet-500/10',
  llm_check: 'border-fuchsia-500/60 bg-fuchsia-500/10',
  actions: 'border-amber-500/60 bg-amber-500/10',
  set_var: 'border-cyan-500/60 bg-cyan-500/10',
  call_function: 'border-orange-500/60 bg-orange-500/10',
  call_rule: 'border-rose-500/60 bg-rose-500/10',
  end: 'border-muted-foreground/40 bg-muted/30',
}
