import {
  defaultActionForDomain,
  defaultConditionFieldForDomain,
  defaultTriggerForDomain,
} from './domains'
import type { AutomationDomain } from './domains'
import type {
  ActionItem,
  AutomationWorkflow,
  RuleEditorState,
  WorkflowEdgeDef,
  WorkflowNodeDef,
} from './types'

let nodeCounter = 0

export function nextNodeId(prefix: string) {
  nodeCounter += 1
  return `${prefix}-${Date.now()}-${nodeCounter}`
}

export function createDefaultWorkflow(
  kind: 'rule' | 'function' = 'rule',
  domain: AutomationDomain = 'mail'
): AutomationWorkflow {
  const startId = nextNodeId('start')
  const condId = nextNodeId('cond')
  const actionsId = nextNodeId('actions')
  const endId = nextNodeId('end')
  const conditionField = defaultConditionFieldForDomain(domain)
  const defaultAction = defaultActionForDomain(domain)

  const nodes: WorkflowNodeDef[] = [
    { id: startId, type: 'start', position: { x: 80, y: 200 }, data: {} },
    {
      id: condId,
      type: 'condition',
      position: { x: 280, y: 180 },
      data: { field: conditionField, operator: 'contains', value: '' },
    },
    {
      id: actionsId,
      type: 'actions',
      position: { x: 520, y: 160 },
      data: { actions: [{ type: defaultAction, value: '' }] },
    },
    { id: endId, type: 'end', position: { x: 760, y: 200 }, data: {} },
  ]

  const edges: WorkflowEdgeDef[] = [
    { id: nextNodeId('e'), source: startId, target: condId },
    { id: nextNodeId('e'), source: condId, target: actionsId, sourceHandle: 'true' },
    { id: nextNodeId('e'), source: actionsId, target: endId },
    { id: nextNodeId('e'), source: condId, target: endId, sourceHandle: 'false' },
  ]

  return {
    version: 1,
    kind,
    triggers: {
      operator: 'or',
      groups: [
        {
          operator: 'and',
          items: [defaultTriggerForDomain(domain)],
        },
      ],
    },
    variables: [],
    nodes,
    edges,
  }
}

export function createDefaultRuleEditorState(
  kind: 'rule' | 'function' = 'rule'
): RuleEditorState {
  return {
    name: kind === 'function' ? 'Nouvelle fonction' : 'Nouvelle règle',
    priority: 0,
    is_active: true,
    rule_kind: kind,
    workflow: createDefaultWorkflow(kind),
  }
}

export function createEmptyAction(domain: AutomationDomain = 'mail'): ActionItem {
  return { type: defaultActionForDomain(domain), value: '' }
}

export const DEFAULT_SIMULATION_DRIVE_FILE = {
  file_name: 'document.pdf',
  file_path: '/Documents/document.pdf',
  mime_type: 'application/pdf',
  file_size: 102400,
  is_folder: false,
}

export const DEFAULT_SIMULATION_CONTACT = {
  name: 'Alice Example',
  email: 'alice@example.com',
  phone: '+33 6 00 00 00 00',
  org: 'Example Corp',
  labels: [] as string[],
}

export const DEFAULT_SIMULATION_CALENDAR_EVENT = {
  title: 'Réunion équipe',
  location: 'Salle A',
  organizer: 'alice@example.com',
  attendee: 'bob@example.com',
  start: '2026-06-15T10:00:00Z',
  end: '2026-06-15T11:00:00Z',
  all_day: false,
  has_video: true,
  calendar_id: 'work',
}

export const DEFAULT_SIMULATION_MESSAGE = {
  from: 'expediteur@example.com',
  to: ['moi@example.com'],
  subject: 'Exemple de sujet',
  body_text: 'Contenu du message de test.',
  has_attachments: false,
  labels: [] as string[],
}

export function workflowToApiPayload(state: RuleEditorState) {
  return {
    name: state.name.trim(),
    priority: state.priority,
    is_active: state.is_active,
    rule_kind: state.rule_kind,
    account_id: state.account_id,
    conditions: [],
    actions: [],
    workflow: state.workflow,
  }
}
