export type Folder = {
  name: string;
  path: string;
  abs_path: string;
  is_system: boolean;
  color: number;
  rgb: string;
  share_owner_id: string;
  share_owner_email: string;
  permissions: string;
  flags: string[];
  delimiter: string;
  unread_count: number;
  total_count: number;
  shared: FolderShare[];
};

export type FolderShare = {
  email: string;
  grant_type: string;
  permissions: string;
  display_name: string;
  status: string;
};

export type NativeZimbraShareEntry = FolderShare & {
  resource_type: string;
  resource_id: string;
  resource_name: string;
  resource_path: string;
  is_system: boolean;
};

export type NativeZimbraTask = {
  id: string;
  invite_id: string;
  folder_id: string;
  folder_name: string;
  subject: string;
  location: string;
  description: string;
  due: string;
  start: string;
  completed: string;
  reminder_at: string;
  reminderAt?: string;
  status: string;
  percent_complete: number;
  priority: string;
  reminder_minutes: number;
  html_link: string;
};

export type NativeZimbraTaskList = {
  id: string;
  name: string;
  abs_path: string;
  is_system: boolean;
  remote_id?: string;
  share_owner_id?: string;
  share_owner_email?: string;
  permissions?: string;
  color: number;
  rgb: string;
  shared: FolderShare[];
  total_count: number;
};

export type GoogleTask = {
  id: string;
  list_id: string;
  title: string;
  notes: string;
  status: "needsAction" | "completed" | string;
  due: string;
  completed: string;
  updated: string;
  parent: string;
  position: string;
  self_link: string;
  html_link: string;
};

export type GoogleTaskList = {
  id: string;
  title: string;
  updated: string;
  self_link: string;
  color: number;
  rgb: string;
};

export type ShareNotification = {
  id: string;
  status: string;
  date: string;
  grantor_id: string;
  grantor_email: string;
  grantor_name: string;
  item_id: string;
  item_uuid: string;
  item_name: string;
  item_view: string;
  item_perm: string;
};

export type Tag = {
  id: string;
  name: string;
  color: number;
  unread_count: number;
  total_count: number;
  rgb: string;
};

export type Contact = {
  id: string;
  folder_id: string;
  full_name: string;
  first_name: string;
  last_name: string;
  email: string;
  company: string;
  notes: string;
  phone: string;
  date: string;
  photo_url: string;
};

export type ContactFolder = {
  id: string;
  name: string;
  abs_path: string;
  is_system: boolean;
  color: number;
  rgb: string;
  share_owner_id: string;
  share_owner_email: string;
  permissions: string;
  total_count: number;
  shared: FolderShare[];
};

export type MailQuota = {
  available: boolean;
  used_kb: number;
  limit_kb: number;
  percent: number;
  root: string;
};

export type MailLimits = {
  available: boolean;
  source: string;
  max_upload_bytes: number;
  max_message_bytes: number;
  values: Record<string, number>;
};

export type MailSignature = {
  id: string;
  name: string;
  content: string;
  content_html: string;
  content_type: string;
};

export type OutOfOfficePreferences = {
  email: string;
  enabled: boolean;
  reply_mode: string;
  external_mode: string;
  use_time_period: boolean;
  start_date: string;
  end_date: string;
  all_day: boolean;
  create_appointment: boolean;
  appointment_label: string;
  subject: string;
  message: string;
  synced_to_zimbra?: boolean;
};

export type MessageSummary = {
  folder: string;
  uid: string;
  open_uid?: string;
  conversation_count: number;
  conversation_id: string;
  subject: string;
  sender_name: string;
  sender_email: string;
  to: string;
  date: string;
  flags: string[];
  seen: boolean;
  starred: boolean;
  size: number;
  preview: string;
  is_draft: boolean;
  scheduled_send_at_ms: number;
  high_priority: boolean;
  request_read_receipt: boolean;
  has_attachments: boolean;
  has_calendar_invite: boolean;
  tags: string[];
};

export type MessagePage = {
  folder: string;
  messages: MessageSummary[];
  offset: number;
  limit: number;
  has_more: boolean;
};

export type CalendarInvite = {
  method: string;
  uid: string;
  summary: string;
  description: string;
  location: string;
  organizer: string;
  attendees: string[];
  start: string;
  end: string;
  all_day: boolean;
  reminder_minutes: number;
};

export type MessageDetail = MessageSummary & {
  open_uid: string;
  html: string;
  text: string;
  share_xml: string;
  calendar_invite?: CalendarInvite | null;
  cc: string;
  bcc: string;
  headers: { name: string; value: string }[];
  attachments: { filename: string; content_type: string; size: number; part: string; content_id?: string; inline?: boolean }[];
  conversation_messages: {
    uid: string;
    folder: string;
    subject: string;
    sender_name: string;
    sender_email: string;
    to: string;
    date: string;
    flags: string[];
    seen: boolean;
    starred: boolean;
    size: number;
    preview: string;
    html: string;
    text: string;
    share_xml: string;
    calendar_invite?: CalendarInvite | null;
    has_calendar_invite: boolean;
    cc: string;
    bcc: string;
    headers: { name: string; value: string }[];
    attachments: { filename: string; content_type: string; size: number; part: string; content_id?: string; inline?: boolean }[];
    tags: string[];
  }[];
};

export type AttachmentDraft = {
  filename: string;
  content_type: string;
  content_base64?: string;
  size: number;
  kind?: "upload" | "drive_file" | "drive_link" | "existing";
  part?: string;
  source_uid?: string;
  drive_file_id?: string;
  drive_web_view_link?: string;
  drive_file_name?: string;
};

export type ComposeSeed = {
  to?: string;
  cc?: string;
  bcc?: string;
  subject?: string;
  body?: string;
  body_html?: string;
  focus_target?: "to" | "body";
  origid?: string;
  rt?: "r" | "w";
  draft_id?: string;
  attachments?: AttachmentDraft[];
  high_priority?: boolean;
  request_read_receipt?: boolean;
};

export type SavedSearch = {
  id: string;
  name: string;
  query: string;
  types: string;
  sort_by: string;
};

export type CalendarAttendee = {
  email: string;
  name: string;
  status: string;
  role: string;
  rsvp: boolean;
};

export type CalendarEvent = {
  id: string;
  ical_uid: string;
  summary: string;
  start: string;
  end: string;
  time_zone: string;
  location: string;
  html_link: string;
  description: string;
  description_html: string;
  attendees: string[];
  attendee_details: CalendarAttendee[];
  organizer: string;
  is_organizer: boolean;
  attendee_status: string;
  all_day: boolean;
  repeat_pattern: "none" | "daily" | "weekly" | "monthly" | "yearly";
  repeat_interval: number;
  repeat_end_mode: "never" | "on_date" | "after_occurrences";
  repeat_until: string;
  repeat_occurrences: number;
  recurring: boolean;
  reminder_minutes: number;
  calendar_id: string;
  calendar_summary: string;
};

export type CalendarReminderNotification = {
  id: string;
  calendar_id: string;
  calendar_summary: string;
  event_id: string;
  due_at: string;
  created_at: string;
  event: CalendarEvent;
};

export type CalendarSource = {
  id: string;
  summary: string;
  primary: boolean;
  background_color: string;
  foreground_color: string;
  access_role: string;
  color?: number;
  rgb?: string;
  remote_id?: string;
  share_owner_id?: string;
  share_owner_email?: string;
  permissions?: string;
};

export type CalendarStatus = {
  configured: boolean;
  oauth_configured: boolean;
  public_configured: boolean;
  connected: boolean;
  writable: boolean;
  mode: "oauth" | "public" | "none";
};

export type ProductChannelConfig = {
  provider: "google" | "native_zimbra" | "zimbra" | "none" | string;
  enabled: boolean;
  planned?: boolean;
};

export type ProductFeatureConfig = {
  enabled: boolean;
  visible: boolean;
  locked: boolean;
  required_tier?: string;
};

export type ProductConfig = {
  edition: string;
  tier?: string;
  source_sku?: string;
  name: string;
  providers: {
    mail: string;
    calendar: string;
    task?: string;
    drive: string;
  };
  capabilities: {
    mail: boolean;
    contacts: boolean;
    calendar: boolean;
    drive: boolean;
    calendar_google: boolean;
    drive_google: boolean;
    tasks_google: boolean;
    calendar_native_zimbra: boolean;
    drive_native_zimbra: boolean;
    tasks_native_zimbra: boolean;
    share_mail_folder?: boolean;
    share_contact_folder?: boolean;
  };
  features?: Record<string, ProductFeatureConfig>;
  planned: {
    mail: boolean;
    calendar: boolean;
    drive: boolean;
  };
};

export type BrandingConfig = {
  name: string;
  tagline: string;
  logo_url: string;
  logo_alt: string;
  favicon_url: string;
  login_theme?: string;
  login_landscape_only?: boolean;
  login_design_mode?: "theme" | "custom";
  login_template?: "panel-left" | "panel-right" | "centered" | "split";
  login_background_color?: string;
  login_gradient_color?: string;
  login_brand_name_color?: string;
  login_tagline_color?: string;
  login_supporting_text_color?: string;
  login_background_url?: string;
  login_box_transparency?: number;
};

export type LicenseStatusConfig = {
  required: boolean;
  configured: boolean;
  status: string;
  message: string;
  admin_warning: boolean;
  user_warning: boolean;
  hard_block: boolean;
  activation_required?: boolean;
  activation_status?: string;
  activated?: boolean;
  activation_blocked?: boolean;
  activation_grace_days?: number;
  activation_grace_until?: string | null;
  support_upgrade_active: boolean;
  over_deployment: boolean;
  over_quota: boolean;
  server_reachable: boolean;
  server_url?: string;
  cache_used: boolean;
  checked_at: string;
  last_error?: string;
  license_key?: string;
  edition?: string;
  tier?: string;
  license_model?: "deployment" | "deployment_mailbox" | string;
  allowed_deployments?: number;
  deployment_count?: number;
  mailbox_quota?: number | null;
  active_mailboxes?: number;
  warning_started_at?: string | null;
  days_in_warning?: number;
  warning_severity?: "none" | "admin" | "user_banner" | "user_strong" | "user_fullscreen" | string;
  support_upgrade_until?: string | null;
  status_token?: string;
};

export type AppConfig = {
  defaults: Record<string, string | number | boolean>;
  branding: BrandingConfig;
  admin_help_text?: string;
  product: ProductConfig;
  license?: LicenseStatusConfig;
  calendar_configured: boolean;
  drive_configured: boolean;
  native_zimbra_configured?: boolean;
  billing_configured: boolean;
  imapsync_configured: boolean;
};

export type LoginResponse = {
  email: string;
  two_factor_required: boolean;
  two_factor_setup_required?: boolean;
  challenge_token: string;
  challenge_expires_in: number;
  session_expires_at: number;
  session_ttl_seconds: number;
};

export type AuthSession = {
  email: string;
  provider: string;
  two_factor_setup_required?: boolean;
  session_expires_at: number;
  session_ttl_seconds: number;
};

export type MailboxProfile = {
  email: string;
  display_name: string;
  first_name: string;
  surname: string;
  photo_url: string;
};

export type TwoFactorStatus = {
  email: string;
  enabled: boolean;
  required?: boolean;
  configured: boolean;
  issuer: string;
  secret: string;
  provisioning_uri: string;
  last_used_step: number;
  recovery_codes_remaining: number;
  created_at: string;
  updated_at: string;
};

export type TwoFactorRecoveryCodes = {
  email: string;
  recovery_codes: string[];
  recovery_codes_remaining: number;
};

export type WorkspacePreferences = {
  email: string;
  drive_save_folder_id: string;
  drive_save_folder_name: string;
  message_view_mode: "conversation" | "message";
  theme: string;
  font_scale: string;
  time_zone: string;
  locale: string;
};

export type MailFilterRule = {
  id: string;
  name: string;
  enabled: boolean;
  field: "from" | "to" | "subject" | "body";
  operator: "contains" | "equals" | "starts_with" | "ends_with";
  value: string;
  target_folder_id: string;
  target_folder_name: string;
  stop_processing: boolean;
  order: number;
};

export type WorkspaceFilters = {
  email: string;
  rules: MailFilterRule[];
};

export type MailMigrationJob = {
  id: string;
  organization_id: string;
  mailbox_email: string;
  source_type: string;
  source_host: string;
  source_port: number;
  source_security: "ssl" | "starttls" | "plain";
  source_username: string;
  status: string;
  phase: string;
  dry_run: boolean;
  options: Record<string, unknown>;
  copied_messages: number;
  failed_messages: number;
  processed_messages: number;
  last_error: string;
  log_excerpt: string;
  result_filename: string;
  result_media_type: string;
  started_at: string;
  finished_at: string;
  created_at: string;
  updated_at: string;
};

export type MailMigrationConnectionResult = {
  ok: boolean;
  message: string;
  folders: string[];
  destination_host: string;
  destination_port: number;
};

export type MailImportResult = {
  ok: boolean;
  imported: number;
  failed: number;
  folder: string;
  source_type: string;
  message: string;
};

export type DriveFile = {
  id: string;
  name: string;
  mime_type: string;
  size: number;
  item_count: number;
  total_size: number;
  modified_time: string;
  web_view_link: string;
  web_content_link: string;
  icon_link: string;
  trashed: boolean;
  parent_ids: string[];
  share_owner_id: string;
  share_owner_email: string;
  permissions: string;
  color: number;
  rgb: string;
};

export type DriveStatus = {
  configured: boolean;
  oauth_configured: boolean;
  connected: boolean;
  writable: boolean;
  mode: "oauth" | "none";
};

export type BillingCapabilities = {
  provider: string;
  recurring: boolean;
  customer_admin: boolean;
  invoices: boolean;
  local_methods: boolean;
  card_payments: boolean;
  webhooks: boolean;
};

export type BillingProviderInfo = {
  provider: string;
  capabilities: BillingCapabilities;
};

export type Organization = {
  id: string;
  name: string;
  primary_domain: string;
  status: string;
  timezone: string;
  billing_customer_id: string;
  billing_subscription_id: string;
  billing_provider: string;
  default_plan_code: string;
  seat_quantity: number;
  mailbox_limit: number;
  created_at: string;
  updated_at: string;
};

export type TenantAdminLoginResponse = {
  email: string;
  admin_token: string;
  two_factor_required: boolean;
  two_factor_setup_required?: boolean;
  challenge_token: string;
  challenge_expires_in: number;
  session_expires_at: number;
  session_ttl_seconds: number;
};

export type Domain = {
  id: string;
  organization_id: string;
  domain_name: string;
  status: string;
  verified_at: string;
  dns_txt_token: string;
  mx_status: string;
  spf_status: string;
  dkim_status: string;
  dmarc_status: string;
  created_at: string;
  updated_at: string;
};

export type Mailbox = {
  id: string;
  organization_id: string;
  domain_id: string;
  local_part: string;
  email: string;
  display_name: string;
  status: string;
  quota_mb: number;
  role: string;
  created_at: string;
  updated_at: string;
  quota_used_kb?: number;
  quota_limit_kb?: number;
  quota_percent?: number;
  last_login_at?: string;
  last_login_days?: number;
  account_status?: string;
  is_postmaster?: boolean;
  is_domain_admin?: boolean;
  two_factor_enabled?: boolean;
  two_factor_required?: boolean;
  public_mail_url?: string;
  aliases?: string[];
  forward_addresses?: string[];
  forward_keep_local_copy?: boolean;
  forward_allowed?: boolean;
};

export type DistributionList = {
  name: string;
  account_id: string;
  display_name: string;
  member_count: number;
  members: string[];
};

export type Subscription = {
  id: string;
  organization_id: string;
  provider: string;
  provider_customer_id: string;
  provider_subscription_id: string;
  price_code: string;
  quantity: number;
  status: string;
  current_period_start: string;
  current_period_end: string;
  cancel_at_period_end: boolean;
  latest_invoice_id: string;
  created_at: string;
  updated_at: string;
};

export type InvoiceRecord = {
  id: string;
  organization_id: string;
  provider_invoice_id: string;
  amount_due: number;
  currency: string;
  status: string;
  period_start: string;
  period_end: string;
  due_at: string;
  paid_at: string;
  created_at: string;
  updated_at: string;
};

export type BillingEvent = {
  id: string;
  organization_id: string;
  provider: string;
  event_type: string;
  status: string;
  payload: Record<string, unknown>;
  created_at: string;
};

export type ProvisioningAction = {
  resource_type: string;
  resource_id: string;
  action: string;
  status: string;
  message: string;
};

export type DnsInstruction = {
  host: string;
  type: string;
  value: string;
  priority: string;
  purpose: string;
};

export type ProvisioningSummary = {
  provider: string;
  event_type: string;
  handled: boolean;
  action: string;
  message: string;
  organization_id: string;
  organization_status: string;
  subscription_status: string;
  onboarding_email_subject: string;
  onboarding_email_body: string;
  dns_instructions: DnsInstruction[];
  actions: ProvisioningAction[];
  organization: Organization | Record<string, unknown>;
  subscription: Subscription | Record<string, unknown>;
  domains: Domain[];
  mailboxes: Mailbox[];
  billing_event: BillingEvent | Record<string, unknown>;
  provisioning_events: Array<Record<string, unknown>>;
};
