export interface ReminderStep {
  step: number
  delayDays: number
  channel: 'email' | 'sms' | 'whatsapp'
  subject: string
  templateKey: string
  description: string
}

export const DEFAULT_REMINDER_SEQUENCE: ReminderStep[] = [
  {
    step: 0,
    delayDays: 0,
    channel: 'email',
    subject: 'Your pledge payment details',
    templateKey: 'instructions',
    description: 'Payment instructions with bank details and reference',
  },
  {
    step: 1,
    delayDays: 2,
    channel: 'email',
    subject: 'Quick reminder about your pledge',
    templateKey: 'gentle_nudge',
    description: 'Gentle nudge - "We noticed your pledge hasn\'t been received yet"',
  },
  {
    step: 2,
    delayDays: 7,
    channel: 'email',
    subject: 'Your pledge is making a difference',
    templateKey: 'urgency_impact',
    description: 'Impact story + urgency - "Your £X helps fund..."',
  },
  {
    step: 3,
    delayDays: 14,
    channel: 'email',
    subject: 'Final reminder about your pledge',
    templateKey: 'final_reminder',
    description: 'Final reminder with easy cancel option',
  },
]

/**
 * Calculate reminder schedule dates from pledge creation
 */
export function calculateReminderSchedule(
  pledgeCreatedAt: Date,
  sequence: ReminderStep[] = DEFAULT_REMINDER_SEQUENCE
): Array<{ step: number; scheduledAt: Date; channel: string; templateKey: string; subject: string }> {
  return sequence.map((s) => ({
    step: s.step,
    scheduledAt: new Date(pledgeCreatedAt.getTime() + s.delayDays * 24 * 60 * 60 * 1000),
    channel: s.channel,
    templateKey: s.templateKey,
    subject: s.subject,
  }))
}

/**
 * Generate reminder message content from template
 */
export function generateReminderContent(
  templateKey: string,
  variables: {
    donorName?: string
    amount: string
    reference: string
    bankName?: string
    sortCode?: string
    accountNo?: string
    accountName?: string
    eventName: string
    pledgeUrl: string
    cancelUrl: string
  }
): { subject: string; body: string } {
  const name = variables.donorName || 'there'
  const templates: Record<string, { subject: string; body: string }> = {
    instructions: {
      subject: `Payment details for your £${variables.amount} pledge`,
      body: `Hi ${name},\n\nThank you for pledging £${variables.amount} at ${variables.eventName}!\n\nTo complete your donation, please transfer to:\n\nBank: ${variables.bankName || 'See details'}\nSort Code: ${variables.sortCode || 'N/A'}\nAccount: ${variables.accountNo || 'N/A'}\nName: ${variables.accountName || 'N/A'}\nReference: ${variables.reference}\n\n⚠️ Please use the exact reference above so we can match your payment.\n\nView your pledge: ${variables.pledgeUrl}\n\nThank you for your generosity!`,
    },
    gentle_nudge: {
      subject: `Quick reminder: your £${variables.amount} pledge`,
      body: `Hi ${name},\n\nJust a friendly reminder about your £${variables.amount} pledge at ${variables.eventName}.\n\nIf you've already sent the payment, thank you! It can take a few days to appear.\n\nIf not, here's your reference: ${variables.reference}\n\nView details: ${variables.pledgeUrl}\n\nNo longer wish to donate? ${variables.cancelUrl}`,
    },
    urgency_impact: {
      subject: `Your £${variables.amount} pledge is making a difference`,
      body: `Hi ${name},\n\nYour £${variables.amount} pledge from ${variables.eventName} is still outstanding.\n\nEvery donation makes a real impact. Your contribution helps us continue our vital work.\n\nPayment reference: ${variables.reference}\nView details: ${variables.pledgeUrl}\n\nNeed help? Just reply to this email.\nCancel pledge: ${variables.cancelUrl}`,
    },
    final_reminder: {
      subject: `Final reminder: £${variables.amount} pledge`,
      body: `Hi ${name},\n\nThis is our final reminder about your £${variables.amount} pledge from ${variables.eventName}.\n\nWe understand circumstances change. If you'd like to:\n✅ Pay now - use reference: ${variables.reference}\n❌ Cancel - ${variables.cancelUrl}\n\nView details: ${variables.pledgeUrl}\n\nThank you for considering us.`,
    },
  }

  return templates[templateKey] || templates.instructions
}
