import 'server-only';

export type EmailProviderConfig = {
  providerType: 'smtp';
  host: string;
  port: number;
  secure: boolean;
  username: string | null;
  password: string | null;
  fromEmail: string;
  fromName: string | null;
  replyToEmail: string | null;
};

export type EmailTestResult = {
  ok: boolean;
  detail?: string;
};

export type SendEmailRecipient = {
  email: string;
  name?: string | null;
};

export type SendEmailAttachment = {
  fileName: string;
  contentType: string;
  buffer: Buffer;
};

export type SendEmailInput = {
  subject: string;
  text: string;
  html?: string | null;
  to: SendEmailRecipient[];
  cc?: SendEmailRecipient[];
  bcc?: SendEmailRecipient[];
  replyTo?: string | null;
  attachments?: SendEmailAttachment[];
};

export type SendEmailResult = {
  providerMessageId: string | null;
  acceptedRecipients: string[];
  rejectedRecipients: string[];
  metadata?: Record<string, unknown>;
};

export interface EmailProvider {
  testConnection(config: EmailProviderConfig): Promise<EmailTestResult>;
  sendEmail(config: EmailProviderConfig, input: SendEmailInput): Promise<SendEmailResult>;
}
