import {
  workflow,
  collectFirstName,
  collectEmail,
  saveToContact,
  sendMessage,
  handoffToHuman,
  createTask,
  addTags,
  wait,
  sendEmail,
} from '@candoa/workflows'

// Customer onboarding workflow
export const customerOnboarding = workflow('customer-onboarding', {
  description: 'Complete customer onboarding process with data collection',
})
  .on('user.signup')
  .use(
    sendMessage({
      message: "Welcome! Let's get you set up in just a few steps.",
    }),
    collectFirstName({ prompt: "First, what's your first name?" }),
    collectEmail({ prompt: "What's your email address?" }),
    saveToContact({ field: 'firstName', value: '{{firstName}}' }),
    saveToContact({ field: 'email', value: '{{email}}' }),
    sendMessage({ message: 'Perfect! Welcome aboard {{firstName}} 🎉' }),
    addTags({ tags: ['onboarded'], target: 'contact' }),
    createTask({
      title: 'Send welcome materials to {{firstName}}',
      assignee: 'onboarding-team',
    }),
  )

// Support escalation for negative sentiment
export const supportEscalation = workflow('support-escalation', {
  description: 'Escalate negative sentiment to human agents immediately',
})
  .on('sentiment.negative')
  .use(
    sendMessage({
      message:
        'I sense you might be frustrated. Let me get you connected with a human agent.',
    }),
    handoffToHuman({
      reason: 'Negative sentiment detected',
    }),
    createTask({
      title: 'Urgent: Frustrated customer needs assistance',
    }),
    addTags({ tags: ['escalated', 'urgent'], target: 'conversation' }),
  )

// Payment failure recovery workflow
export const paymentRecovery = workflow('payment-recovery', {
  description: 'Automated payment failure recovery with email follow-up',
})
  .on('payment.failed')
  .use(
    sendMessage({
      message:
        'It looks like there was an issue with your payment. Let me help you resolve this.',
    }),
    wait({ duration: 1, unit: 'hours' }),
    sendEmail({
      to: '{{email}}',
      subject: 'Payment Issue - Action Required',
      body: 'Hi {{firstName}}, we had trouble processing your payment. Please update your payment method.',
    }),
    createTask({
      title: 'Follow up on failed payment for {{firstName}}',
      description: 'Payment failed, email sent, needs follow-up',
    }),
  )

// Lead qualification workflow
export const leadQualification = workflow('lead-qualification', {
  description: 'Qualify leads by collecting basic information',
})
  .on('message.received')
  .use(
    sendMessage({
      message: "Hi there! I'd love to learn more about your needs.",
    }),
    collectFirstName({ prompt: "What's your first name?" }),
    collectEmail({ prompt: "What's your email address?" }),
    saveToContact({ field: 'firstName', value: '{{firstName}}' }),
    saveToContact({ field: 'email', value: '{{email}}' }),
    sendMessage({
      message:
        'Thanks {{firstName}}! Someone from our team will be in touch soon.',
    }),
    addTags({ tags: ['qualified-lead'], target: 'contact' }),
    createTask({
      title: 'Follow up with qualified lead: {{firstName}}',
      description: 'New qualified lead collected via chat',
    }),
  )
