import type { Email } from '../core/classes.email.js';
import type { IExtendedSmtpSession } from './classes.unified.email.server.js';

/**
 * Route configuration for email routing
 */
export interface IEmailRoute {
  /** Route identifier */
  name: string;
  /** Order of evaluation (higher priority evaluated first, default: 0) */
  priority?: number;
  /** Conditions to match */
  match: IEmailMatch;
  /** Action to take when matched */
  action: IEmailAction;
}

/**
 * Match criteria for email routing
 */
export interface IEmailMatch {
  /** Email patterns to match recipients: "*@example.com", "admin@*" */
  recipients?: string | string[];
  /** Email patterns to match senders */
  senders?: string | string[];
  /** IP addresses or CIDR ranges to match */
  clientIp?: string | string[];
  /** Require authentication status */
  authenticated?: boolean;
  
  // Optional advanced matching
  /** Headers to match */
  headers?: Record<string, string | RegExp>;
  /** Message size range */
  sizeRange?: { min?: number; max?: number };
  /** Subject line patterns */
  subject?: string | RegExp;
  /** Has attachments */
  hasAttachments?: boolean;
}

/**
 * Action to take when route matches
 */
export interface IEmailAction {
  /** Type of action to perform */
  type: 'forward' | 'deliver' | 'reject' | 'process';
  
  /** Forward action configuration */
  forward?: {
    /** Target host to forward to */
    host: string;
    /** Target port (default: 25) */
    port?: number;
    /** Authentication credentials */
    auth?: {
      user: string;
      pass: string;
    };
    /** Preserve original headers */
    preserveHeaders?: boolean;
    /** Additional headers to add */
    addHeaders?: Record<string, string>;
  };
  
  /** Reject action configuration */
  reject?: {
    /** SMTP response code */
    code: number;
    /** SMTP response message */
    message: string;
  };
  
  /** Process action configuration */
  process?: {
    /** Enable content scanning */
    scan?: boolean;
    /** Enable DKIM signing */
    dkim?: boolean;
    /** Delivery queue priority */
    queue?: 'normal' | 'priority' | 'bulk';
  };
  
  /** Options for various action types */
  options?: {
    /** MTA specific options */
    mtaOptions?: {
      domain?: string;
      allowLocalDelivery?: boolean;
      localDeliveryPath?: string;
      dkimSign?: boolean;
      dkimOptions?: {
        domainName: string;
        keySelector: string;
        privateKey?: string;
      };
      smtpBanner?: string;
      maxConnections?: number;
      connTimeout?: number;
      spoolDir?: string;
    };
    /** Content scanning configuration */
    contentScanning?: boolean;
    scanners?: Array<{
      type: 'spam' | 'virus' | 'attachment';
      threshold?: number;
      action: 'tag' | 'reject';
      blockedExtensions?: string[];
    }>;
    /** Email transformations */
    transformations?: Array<{
      type: string;
      header?: string;
      value?: string;
      domains?: string[];
      append?: boolean;
      [key: string]: any;
    }>;
  };
  
  /** Delivery options (applies to forward/process/deliver) */
  delivery?: {
    /** Rate limit (messages per minute) */
    rateLimit?: number;
    /** Number of retry attempts */
    retries?: number;
  };
}

/**
 * Context for route evaluation
 */
export interface IEmailContext {
  /** The email being routed */
  email: Email;
  /** The SMTP session */
  session: IExtendedSmtpSession;
}