export interface EmailRecipientDto {
  name?: string;
  address: string;
}

export interface EmailHeaderDto {
  key: string;
  value: string;
}

export interface EmailAttachmentDto {
  id?: string;
  filename?: string;
  contentType?: string;
  encoding?: string;
  contentTransferEncoding?: string;
  contentDisposition?: "inline" | "attachment";
  content: string;
  cid?: string;
}

export interface DraftReferenceDto {
  mailbox: string;
  id: number;
}

export interface EmailEnvelopeDto {
  from: EmailRecipientDto;
  to: EmailRecipientDto[];
}

export interface SendEmailDto {
  mailbox?: string;
  from: EmailRecipientDto;
  replyTo?: EmailRecipientDto;
  to: EmailRecipientDto[];
  cc?: EmailRecipientDto[];
  bcc?: EmailRecipientDto[];
  headers?: EmailHeaderDto[];
  subject?: string;
  text?: string;
  html?: string;
  attachments?: EmailAttachmentDto[];
  meta?: Record<string, unknown>;
  sess?: string;
  ip?: string;
  reference?: Record<string, unknown>;
  isDraft?: boolean;
  draft?: DraftReferenceDto;
  sendTime?: string;
  uploadOnly?: boolean;
  envelope?: EmailEnvelopeDto;
  isForward?: boolean;
}

export interface MessageListQueryDto {
  next?: string;
  previous?: string;
  limit?: number;
  search?: string;
  from?: string;
  subject?: string;
  datestart?: string;
  dateend?: string;
  attachments?: boolean;
  unseen?: boolean;
  flagged?: boolean;
  order?: "asc" | "desc";
}

// Updated types based on actual API response
export interface InboxMessage extends Record<string, unknown> {
  id: number;
  mailbox: string;
  thread: string;
  from: {
    address: string;
    name: string;
  };
  to: Array<{
    address: string;
    name: string;
  }>;
  cc: Array<{
    address: string;
    name: string;
  }>;
  bcc: Array<{
    address: string;
    name: string;
  }>;
  messageId: string;
  subject: string;
  date: string;
  idate: string;
  intro: string;
  attachments: boolean;
  attachmentsList: EmailAttachmentDto[];
  size: number;
  seen: boolean;
  deleted: boolean;
  flagged: boolean;
  draft: boolean;
  answered: boolean;
  forwarded: boolean;
  references: string[];
  contentType: {
    value: string;
    params: {
      boundary: string;
    };
  };
}

export interface Pager {
  page: number;
  limit: number;
  totalItems: number;
  totalPages: number;
  previousCursor: string | boolean;
  nextCursor: string | boolean;
}

export interface InboxResponse {
  statusCode: number;
  success: boolean;
  data: {
    pager: Pager;
    message: string;
    results: InboxMessage[];
  };
}

// Legacy types (keeping for backward compatibility)
export interface MessageDto {
  id: string;
  mailbox: string;
  uid: number;
  flags: string[];
  envelope: {
    date: string;
    subject: string;
    from: EmailRecipientDto[];
    to: EmailRecipientDto[];
    cc?: EmailRecipientDto[];
    bcc?: EmailRecipientDto[];
    messageId: string;
    inReplyTo?: string;
    references?: string[];
  };
  bodyStructure: Record<string, unknown>;
  source?: string;
  text?: string;
  html?: string;
  attachments?: EmailAttachmentDto[];
  headers?: EmailHeaderDto[];
  size: number;
  created: string;
  updated: string;
}

export interface MessageListResponse {
  messages: MessageDto[];
  total: number;
  page: number;
  limit: number;
  totalPages: number;
}

export interface UserMailboxMessageParamDto {
  messageId: string;
}

// New interface based on actual API response
export interface MessageDetailResponse {
  statusCode: number;
  success: boolean;
  data: {
    message: {
      success: boolean;
      id: number;
      mailbox: string;
      thread: string;
      user: string;
      envelope: {
        from: string;
        rcpt: Array<{
          value: string;
          formatted: string;
        }>;
      };
      from: {
        address: string;
        name: string;
      };
      to: Array<{
        address: string;
        name: string;
      }>;
      subject: string;
      messageId: string;
      date: string;
      idate: string;
      size: number;
      seen: boolean;
      deleted: boolean;
      flagged: boolean;
      draft: boolean;
      answered: boolean;
      forwarded: boolean;
      html: string[];
      text: string;
      attachments: EmailAttachmentDto[];
      references: string[];
      metaData: Record<string, unknown>;
      verificationResults: {
        tls: {
          name: string;
          standardName: string;
          version: string;
        };
        spf: string;
        dkim: string;
        arc: boolean;
        bimi: boolean;
      };
      contentType: {
        value: string;
        params: {
          boundary: string;
        };
      };
    };
  };
}

export interface MessageDetail {
  success: boolean;
  id: number;
  mailbox: string;
  mailboxName?: string;

  thread: string;
  user: string;
  envelope: {
    from: string;
    rcpt: Array<{
      value: string;
      formatted: string;
    }>;
  };
  from: {
    address: string;
    name: string;
  };
  to: Array<{
    address: string;
    name: string;
  }>;
  subject: string;
  messageId: string;
  date: string;
  idate: string;
  size: number;
  seen: boolean;
  deleted: boolean;
  flagged: boolean;
  draft: boolean;
  answered: boolean;
  forwarded: boolean;
  html: string[];
  text: string;
  attachments: EmailAttachmentDto[];
  references: string[];
  metaData: Record<string, unknown>;
  verificationResults: {
    tls: {
      name: string;
      standardName: string;
      version: string;
    };
    spf: string;
    dkim: string;
    arc: boolean;
    bimi: boolean;
  };
  contentType: {
    value: string;
    params: {
      boundary: string;
    };
  };
}

export enum BulkActionType {
  SEEN = "seen",
  DELETE = "delete",
  ARCHIVE = "archive",
  FAVORITE = "favorite",
  UNFAVORITE = "unfavorite",
  UNARCHIVE = "unarchive",
  RESTORE = "restore",
  JUNK = "junk",
  TRASH = "trash",
}

// Interface for bulk action request
export interface BulkActionRequest {
  messageIds: number[];
  action: BulkActionType;
  mailbox: string;
}

export interface SingleActionRequest {
  messageId: string;
  mailbox: string;
}

export interface SummarizeEmailRequest {
  messageId: number;
  mailbox: string;
  length?: "medium" | "short" | "long";
  tone?: "professional" | "casual" | "formal";
}

export interface GenerateEmailRequest {
  prompt: string;
}
