import type { ActionType, ConditionField, ConditionOperator } from './types'

export const LABEL_CONDITION_OPERATORS: { value: ConditionOperator; label: string }[] = [
  { value: 'has', label: 'Possède le libellé' },
  { value: 'not_has', label: 'Ne possède pas le libellé' },
]

export const STRING_CONDITION_OPERATORS: { value: ConditionOperator; label: string }[] = [
  { value: 'contains', label: 'contient' },
  { value: 'not_contains', label: 'ne contient pas' },
  { value: 'equals', label: 'égal à' },
  { value: 'starts_with', label: 'commence par' },
  { value: 'ends_with', label: 'finit par' },
  { value: 'regex', label: 'correspond à la regex' },
  { value: 'not_regex', label: 'ne correspond pas à la regex' },
]

export const ATTACHMENT_CONDITION_OPERATORS: { value: ConditionOperator; label: string }[] = [
  { value: 'equals', label: 'égal à' },
]

export function isStringConditionField(field: ConditionField): boolean {
  return (
    field === 'from' ||
    field === 'to' ||
    field === 'subject' ||
    field === 'body' ||
    field === 'drive_file_name' ||
    field === 'drive_file_path' ||
    field === 'drive_mime_type' ||
    field === 'contact_name' ||
    field === 'contact_email' ||
    field === 'contact_phone' ||
    field === 'contact_org' ||
    field === 'calendar_event_title' ||
    field === 'calendar_event_location' ||
    field === 'calendar_event_organizer' ||
    field === 'calendar_event_attendee'
  )
}

export function isLabelConditionField(field: ConditionField): boolean {
  return field === 'label' || field === 'contact_label'
}

export function isBooleanConditionField(field: ConditionField): boolean {
  return (
    field === 'has_attachment' ||
    field === 'drive_is_folder' ||
    field === 'calendar_event_all_day' ||
    field === 'calendar_event_has_video'
  )
}

export function isNumericConditionField(field: ConditionField): boolean {
  return field === 'drive_file_size'
}

export function operatorsForConditionField(field: ConditionField): ConditionOperator[] {
  if (isLabelConditionField(field)) return ['has', 'not_has']
  if (isBooleanConditionField(field)) return ['equals']
  if (isNumericConditionField(field)) return ['equals', 'contains']
  return STRING_CONDITION_OPERATORS.map((o) => o.value)
}

export function operatorOptionsForConditionField(field: ConditionField) {
  if (isLabelConditionField(field)) return LABEL_CONDITION_OPERATORS
  if (isBooleanConditionField(field) || isNumericConditionField(field)) return ATTACHMENT_CONDITION_OPERATORS
  return STRING_CONDITION_OPERATORS
}

export function defaultOperatorForField(field: ConditionField): ConditionOperator {
  if (isLabelConditionField(field)) return 'has'
  if (isBooleanConditionField(field) || isNumericConditionField(field)) return 'equals'
  return 'contains'
}

export function formatConditionSummary(data: {
  field: ConditionField
  operator: ConditionOperator
  value: string
}, fieldLabel: (f: string) => string, opLabel: (o: string) => string): string {
  if (isLabelConditionField(data.field)) {
    const prefix = data.operator === 'not_has' ? 'Sans libellé' : 'Libellé'
    return `${prefix} « ${data.value || '…'} »`
  }
  if (isBooleanConditionField(data.field)) {
    return `${fieldLabel(data.field)} = ${data.value === 'false' ? 'non' : 'oui'}`
  }
  return `${fieldLabel(data.field)} ${opLabel(data.operator)} « ${data.value || '…'} »`
}

export type AutomationSuggestionKind =
  | 'label'
  | 'folder'
  | 'folder_id'
  | 'drive_path'
  | 'mime_type'
  | 'contact_label'
  | 'email'
  | 'calendar_id'
  | 'webhook'
  | 'none'

export function conditionValueSuggestionKind(
  field: ConditionField,
  _operator: ConditionOperator
): AutomationSuggestionKind {
  if (field === 'label') return 'label'
  if (field === 'contact_label') return 'contact_label'
  if (field === 'from' || field === 'to' || field === 'contact_email') return 'email'
  if (field === 'calendar_event_organizer' || field === 'calendar_event_attendee') return 'email'
  if (field === 'drive_file_path') return 'drive_path'
  if (field === 'drive_mime_type') return 'mime_type'
  return 'none'
}

export function switchFieldSuggestionKind(field: string): AutomationSuggestionKind {
  return conditionValueSuggestionKind(field as ConditionField, 'contains')
}

export function actionValueSuggestionKind(type: ActionType): AutomationSuggestionKind {
  switch (type) {
    case 'label':
    case 'remove_label':
      return 'label'
    case 'move':
      return 'folder'
    case 'forward':
    case 'send_mail':
    case 'drive_share':
      return 'email'
    case 'drive_move':
    case 'drive_copy':
      return 'drive_path'
    case 'contact_add_label':
    case 'contact_remove_label':
      return 'contact_label'
    case 'calendar_add_attendee':
    case 'calendar_notify_attendees':
      return 'email'
    case 'calendar_update_title':
      return 'none'
    case 'webhook':
      return 'webhook'
    default:
      return 'none'
  }
}

export function triggerValueSuggestionKind(
  field: 'folder_id' | 'label' | 'folder_path' | 'contact_label' | 'calendar_id'
): AutomationSuggestionKind {
  if (field === 'label') return 'label'
  if (field === 'folder_id') return 'folder_id'
  if (field === 'folder_path') return 'drive_path'
  if (field === 'contact_label') return 'contact_label'
  if (field === 'calendar_id') return 'calendar_id'
  return 'none'
}
