/**
 * Email Nurture Sequences Type Definitions
 *
 * Defines types for automated email nurture campaigns and user journey workflows
 */

import type { Language } from '@/lib/services/email/templates';

// =============================================================================
// Enums
// =============================================================================

export enum NurtureSequenceType {
  TRIAL_ONBOARDING = 'TRIAL_ONBOARDING',
  ABANDONED_DEMO = 'ABANDONED_DEMO',
  POST_SIGNUP = 'POST_SIGNUP',
  PRODUCT_EDUCATION = 'PRODUCT_EDUCATION',
  CONVERSION = 'CONVERSION',
}

export enum NurtureEmailStatus {
  PENDING = 'PENDING',
  SCHEDULED = 'SCHEDULED',
  SENT = 'SENT',
  OPENED = 'OPENED',
  CLICKED = 'CLICKED',
  FAILED = 'FAILED',
  SKIPPED = 'SKIPPED',
}

export enum UserSequenceStatus {
  ACTIVE = 'active',
  PAUSED = 'paused',
  COMPLETED = 'completed',
  CANCELLED = 'cancelled',
}

// =============================================================================
// Core Types
// =============================================================================

export interface NurtureSequence {
  id: string;
  name: string;
  nameAr?: string;
  description?: string;
  sequenceType: NurtureSequenceType;
  isActive: boolean;
  triggerEvent: string;
  triggerConditions?: Record<string, unknown>;
  createdBy?: string;
  updatedBy?: string;
  createdAt: Date;
  updatedAt: Date;
}

export interface NurtureSequenceStep {
  id: string;
  sequenceId: string;
  stepNumber: number;
  name: string;
  subjectEn: string;
  subjectAr: string;
  delayHours: number;
  templateKey: string;
  isActive: boolean;
  createdAt: Date;
  updatedAt: Date;
}

export interface UserNurtureSequenceState {
  id: string;
  userId: string;
  sequenceId: string;
  currentStep: number;
  status: UserSequenceStatus;
  startedAt: Date;
  completedAt?: Date;
  pausedAt?: Date;
  cancelledAt?: Date;
  metadata?: Record<string, unknown>;
}

export interface UserNurtureSequenceEmail {
  id: string;
  stateId: string;
  stepId: string;
  userId: string;
  email: string;
  status: NurtureEmailStatus;
  scheduledFor: Date;
  sentAt?: Date;
  openedAt?: Date;
  clickedAt?: Date;
  failedAt?: Date;
  errorMessage?: string;
  metadata?: Record<string, unknown>;
  createdAt: Date;
  updatedAt: Date;
}

// =============================================================================
// Template Context Types
// =============================================================================

export interface TrialOnboardingContext {
  name: string;
  email: string;
  companyName?: string;
  trialEndsAt?: Date;
  language: Language;
  dashboardUrl: string;
}

export interface AbandonedDemoContext {
  name: string;
  email: string;
  requestedDate?: string;
  language: Language;
  rescheduleUrl: string;
}

export interface PostSignupContext {
  name: string;
  email: string;
  packageName?: string;
  language: Language;
  setupUrl: string;
}

export interface ProductEducationContext {
  name: string;
  email: string;
  feature: string;
  language: Language;
  learnMoreUrl: string;
}

export type NurtureTemplateContext =
  | TrialOnboardingContext
  | AbandonedDemoContext
  | PostSignupContext
  | ProductEducationContext;

// =============================================================================
// Service Types
// =============================================================================

export interface StartSequenceOptions {
  userId: string;
  sequenceType: NurtureSequenceType;
  metadata?: Record<string, unknown>;
}

export interface ScheduleEmailOptions {
  stateId: string;
  stepId: string;
  userId: string;
  email: string;
  scheduledFor: Date;
  context: NurtureTemplateContext;
}

export interface ProcessScheduledEmailsResult {
  processed: number;
  sent: number;
  failed: number;
  errors: string[];
}

export interface SequenceStatistics {
  totalSequences: number;
  activeSequences: number;
  totalUsers: number;
  activeUsers: number;
  completedUsers: number;
  totalEmailsSent: number;
  openRate: number;
  clickRate: number;
}

// =============================================================================
// API Request/Response Types
// =============================================================================

export interface CreateSequenceRequest {
  name: string;
  nameAr?: string;
  description?: string;
  sequenceType: NurtureSequenceType;
  triggerEvent: string;
  triggerConditions?: Record<string, unknown>;
  steps: CreateSequenceStepRequest[];
}

export interface CreateSequenceStepRequest {
  name: string;
  subjectEn: string;
  subjectAr: string;
  delayHours: number;
  templateKey: string;
}

export interface UpdateSequenceRequest {
  name?: string;
  nameAr?: string;
  description?: string;
  isActive?: boolean;
  triggerConditions?: Record<string, unknown>;
}

export interface StartSequenceRequest {
  userId: string;
  sequenceType: NurtureSequenceType;
  metadata?: Record<string, unknown>;
}

export interface PauseSequenceRequest {
  userId: string;
  sequenceId: string;
}

export interface ResumeSequenceRequest {
  userId: string;
  sequenceId: string;
}

export interface CancelSequenceRequest {
  userId: string;
  sequenceId: string;
  reason?: string;
}

export interface SequenceStatusResponse {
  state: UserNurtureSequenceState;
  sequence: NurtureSequence;
  currentStep?: NurtureSequenceStep;
  nextEmail?: UserNurtureSequenceEmail;
  emailHistory: UserNurtureSequenceEmail[];
}
