// ============================================================================
// Core domain types for Enterprise Mail Web
// These types are the contract between UI, hooks, services and the future
// backend. Keep them framework-agnostic (no React types here).
// ============================================================================

export type UserRole = 'admin' | 'member' | 'guest'

export interface CurrentUser {
  id: string
  email: string
  displayName: string
  avatar?: string
  role: UserRole
  timezone: string
  locale: string
}

export interface Session {
  /** Non-secret opaque id (never the session token — that only lives in the HttpOnly cookie). */
  id: string
  user: CurrentUser
  createdAt: string
  expiresAt: string
  device?: string
  ipAddress?: string
  current?: boolean
}

export interface EmailAddress {
  name: string
  email: string
  avatar?: string
}

export type AttachmentKind = 'image' | 'pdf' | 'doc' | 'sheet' | 'archive' | 'other'

export interface Attachment {
  id: string
  fileName: string
  sizeBytes: number
  kind: AttachmentKind
  url?: string
}

export type MailFolderId =
  | 'inbox'
  | 'starred'
  | 'snoozed'
  | 'sent'
  | 'drafts'
  | 'spam'
  | 'trash'

export interface MailFolder {
  id: MailFolderId
  name: string
  icon?: string
  unreadCount?: number
}

export interface Label {
  id: string
  name: string
  color: string
}

export type ImportanceLevel = 'normal' | 'high'

export interface EmailMessage {
  id: string
  folder: MailFolderId
  from: EmailAddress
  to: EmailAddress[]
  cc?: EmailAddress[]
  bcc?: EmailAddress[]
  subject: string
  preview: string
  bodyHtml: string
  attachments: Attachment[]
  labels: string[]
  isRead: boolean
  isStarred: boolean
  importance: ImportanceLevel
  sentAt: string
  snoozedUntil?: string | null
  threadId?: string
}

export type DraftStatus = 'idle' | 'saving' | 'saved'

export interface Draft {
  id: string
  to: EmailAddress[]
  cc?: EmailAddress[]
  bcc?: EmailAddress[]
  subject: string
  bodyHtml: string
  attachments: Attachment[]
  createdAt: string
  updatedAt: string
}

export interface Contact {
  id: string
  name: string
  email: string
  phone?: string
  company?: string
  avatar?: string
  notes?: string
}

export interface CalendarEventAttendee {
  name: string
  email: string
  status: 'accepted' | 'declined' | 'pending'
}

export interface CalendarEvent {
  id: string
  title: string
  description?: string
  location?: string
  start: string
  end: string
  allDay?: boolean
  attendees: CalendarEventAttendee[]
  recurrence?: 'none' | 'daily' | 'weekly' | 'monthly' | 'yearly'
  reminderMinutesBefore?: number
  timezone: string
  color?: string
}

export type NotificationKind =
  | 'new-email'
  | 'draft-saved'
  | 'email-sent'
  | 'calendar-reminder'
  | 'session-expired'
  | 'system'

export interface AppNotification {
  id: string
  kind: NotificationKind
  title: string
  message: string
  createdAt: string
  read: boolean
}

// ---------------------------------------------------------------------------
// Search
// ---------------------------------------------------------------------------

export interface ParsedSearchQuery {
  raw: string
  text: string
  from?: string
  to?: string
  subject?: string
  hasAttachment?: boolean
  isUnread?: boolean
  isStarred?: boolean
  after?: string
  before?: string
}

// ---------------------------------------------------------------------------
// Settings
// ---------------------------------------------------------------------------

export type ThemeMode = 'light' | 'dark' | 'system'
export type LayoutDensity = 'comfortable' | 'compact'

export interface AppSettings {
  general: {
    language: string
    timezone: string
    dateFormat: string
    timeFormat: '12h' | '24h'
    density: LayoutDensity
    defaultLandingPage: MailFolderId
  }
  appearance: {
    theme: ThemeMode
    accentColor: string
    layout: LayoutDensity
  }
  inbox: {
    density: LayoutDensity
    readingPane: 'right' | 'bottom' | 'off'
    conversationView: boolean
    senderDisplay: 'name' | 'email' | 'both'
  }
  notifications: {
    email: boolean
    desktop: boolean
    calendar: boolean
  }
  signature: {
    enabled: boolean
    bodyHtml: string
  }
}

// ---------------------------------------------------------------------------
// Result / error handling
// ---------------------------------------------------------------------------

export type AppErrorKind =
  | 'network'
  | 'unauthorized'
  | 'forbidden'
  | 'not-found'
  | 'session-expired'
  | 'rate-limited'
  | 'validation'
  | 'unknown'

export class AppError extends Error {
  kind: AppErrorKind
  constructor(kind: AppErrorKind, message: string) {
    super(message)
    this.kind = kind
    this.name = 'AppError'
  }
}
