// ─── Auth ────────────────────────────────────────────────────────────────────

export interface LoginRequest {
  email: string
  password: string
  totp_code?: string
}

export interface TokenResponse {
  access_token: string
  refresh_token: string
  token_type: string
  expires_in: number
}

export interface TOTPSetupResponse {
  secret: string
  qr_uri: string
  qr_image_base64: string
}

export interface TOTPStatusResponse {
  totp_enabled: boolean
}

// ─── User ─────────────────────────────────────────────────────────────────────

export type UserRole = 'super_admin' | 'admin' | 'supervisor' | 'operator' | 'readonly'

export interface UserResponse {
  id: string
  tenant_id: string
  email: string
  full_name: string
  role: UserRole
  is_active: boolean
  totp_enabled: boolean
  last_login_at: string | null
  created_at: string
  updated_at: string
}

export interface UserListResponse {
  items: UserResponse[]
  total: number
  page: number
  page_size: number
  pages: number
}

export interface UserCreateRequest {
  email: string
  password: string
  full_name: string
  role: UserRole
}

export interface UserUpdateRequest {
  full_name?: string
  role?: UserRole
  is_active?: boolean
}

// ─── Mailbox ──────────────────────────────────────────────────────────────────

export type MailboxStatus = 'active' | 'paused' | 'error' | 'deleted'

export interface MailboxResponse {
  id: string
  tenant_id: string
  email_address: string
  display_name: string | null
  provider: string | null
  imap_host: string
  imap_port: number
  imap_use_ssl: boolean
  smtp_host: string
  smtp_port: number
  smtp_use_tls: boolean
  status: MailboxStatus
  last_sync_at: string | null
  last_sync_uid: number | null
  sync_error_msg: string | null
  sync_error_count: number
  created_by: string | null
  created_at: string
  updated_at: string
}

export interface MailboxListResponse {
  items: MailboxResponse[]
  total: number
  page: number
  page_size: number
}

export interface MailboxCreateRequest {
  email_address: string
  display_name?: string
  provider?: string
  imap_host: string
  imap_port: number
  imap_user: string
  imap_pass: string
  imap_use_ssl: boolean
  smtp_host: string
  smtp_port: number
  smtp_user: string
  smtp_pass: string
  smtp_use_tls: boolean
}

export interface MailboxUpdateRequest {
  display_name?: string
  provider?: string
  status?: 'active' | 'paused'
  imap_host?: string
  imap_port?: number
  imap_user?: string
  imap_pass?: string
  imap_use_ssl?: boolean
  smtp_host?: string
  smtp_port?: number
  smtp_user?: string
  smtp_pass?: string
  smtp_use_tls?: boolean
}

export interface ConnectionTestResult {
  success: boolean
  message: string
  latency_ms: number | null
  capabilities: string[] | null
}

// ─── Message ──────────────────────────────────────────────────────────────────

export type PecDirection = 'inbound' | 'outbound'
export type PecState =
  | 'draft'
  | 'queued'
  | 'sent'
  | 'accepted'
  | 'delivered'
  | 'anomaly'
  | 'failed'
  | 'received'
export type PecMsgType =
  | 'posta_certificata'
  | 'accettazione'
  | 'non_accettazione'
  | 'presa_in_carico'
  | 'avvenuta_consegna'
  | 'mancata_consegna'
  | 'errore_consegna'
  | 'preavviso_mancata_consegna'
  | 'rilevazione_virus'
  | 'unknown'

export interface MessageResponse {
  id: string
  tenant_id: string
  mailbox_id: string
  message_id_header: string | null
  imap_uid: number | null
  imap_folder: string
  direction: PecDirection
  pec_type: PecMsgType
  state: PecState
  subject: string | null
  from_address: string | null
  to_addresses: string[]
  cc_addresses: string[]
  sent_at: string | null
  received_at: string | null
  size_bytes: number | null
  body_text: string | null
  body_html: string | null
  has_attachments: boolean
  parent_message_id: string | null
  is_read: boolean
  is_starred: boolean
  is_archived: boolean
  archived_at: string | null
  raw_eml_path: string | null
  created_at: string
  updated_at: string
}

export interface MessageListResponse {
  items: MessageResponse[]
  total: number
  page: number
  page_size: number
}

// ─── Attachment ───────────────────────────────────────────────────────────────

export interface AttachmentResponse {
  id: string
  message_id: string
  filename: string
  content_type: string | null
  size_bytes: number | null
  storage_path: string
  checksum_sha256: string | null
  created_at: string
}

// ─── Send Job ─────────────────────────────────────────────────────────────────

export type SendJobStatus = 'pending' | 'sending' | 'sent' | 'failed' | 'retrying'

export interface SendJobResponse {
  id: string
  tenant_id: string
  mailbox_id: string
  message_id: string | null
  status: SendJobStatus
  attempt_count: number
  max_attempts: number
  next_retry_at: string | null
  last_error: string | null
  queued_at: string
  sent_at: string | null
  created_by: string | null
}

export interface SendJobListResponse {
  items: SendJobResponse[]
  total: number
  page: number
  page_size: number
}

export interface SendPecRequest {
  mailbox_id: string
  to_addresses: string[]
  cc_addresses?: string[]
  subject: string
  body_text?: string
  body_html?: string
  reply_to_message_id?: string
}

// ─── Permission ───────────────────────────────────────────────────────────────

export interface PermissionResponse {
  id: string
  tenant_id: string
  user_id: string
  mailbox_id: string
  can_read: boolean
  can_send: boolean
  can_manage: boolean
  granted_by: string | null
  granted_at: string
}

export interface PermissionGrantRequest {
  can_read?: boolean
  can_send?: boolean
  can_manage?: boolean
}

export interface MailboxUserPermissionResponse {
  user_id: string
  email: string
  full_name: string
  role: UserRole
  can_read: boolean
  can_send: boolean
  can_manage: boolean
  granted_at: string
}

export interface UserMailboxPermissionResponse {
  mailbox_id: string
  email_address: string
  display_name: string | null
  can_read: boolean
  can_send: boolean
  can_manage: boolean
}

// ─── WebSocket events ─────────────────────────────────────────────────────────

export type WsEventType =
  | 'mailbox:new_message'
  | 'mailbox:status_changed'
  | 'send_job:status_changed'
  | 'send_job:anomaly'

export interface WsEvent {
  type: WsEventType
  tenant_id: string
  payload: Record<string, unknown>
}

// ─── API Pagination ──────────────────────────────────────────────────────────

export interface PaginationParams {
  page?: number
  page_size?: number
}

export interface ApiError {
  detail: string | Array<{ loc: string[]; msg: string; type: string }>
}
