import type {
  MailCredentialEnvelope,
  MailImapTransportSettings,
  MailProfilePatternKey,
  MailProfilePolicy,
  MailProfileScope,
  MailSecurity,
  MailServerProfile,
  MailServerProfileCredentials,
  MailTransportCredentials,
  MailTransportSettings
} from "../types";

export type {
  MailCredentialEnvelope,
  MailCredentialPolicy,
  MailProfilePatternKey,
  MailProfilePolicy,
  MailProfileScope,
  MailSecurity,
  MailServerEndpoint,
  MailServerProfile
} from "../types";

export type MailSmtpTestPayload = MailTransportSettings;
export type MailImapTestPayload = MailImapTransportSettings;
export type MailTransportCredentialsPayload = MailTransportCredentials;
export type MailServerProfileCredentialsPayload = MailServerProfileCredentials;
export type MailServerProfileListResponse = { profiles: MailServerProfile[] };
export type MailCredentialListResponse = { credentials: MailCredentialEnvelope[] };
export type MailProfilePatternRules = Partial<Record<MailProfilePatternKey, string[]>>;

export type MailConnectionTestResponse = {
  ok: boolean;
  protocol: "smtp" | "imap";
  host?: string | null;
  port?: number | null;
  security?: MailSecurity | string | null;
  message: string;
  details?: Record<string, unknown>;
};

export type MailImapFolderResponse = {
  name: string;
  flags?: string[];
  message_count?: number | null;
  unseen_count?: number | null;
};

export type MailImapFolderListResponse = {
  ok: boolean;
  protocol: "imap";
  host?: string | null;
  port?: number | null;
  security?: MailSecurity | string | null;
  message: string;
  folders: MailImapFolderResponse[];
  detected_sent_folder?: string | null;
  from_cache?: boolean;
  refreshing?: boolean;
  indexed_at?: string | null;
  details?: Record<string, unknown>;
};

export const mailProfilePatternKeys = [
  "smtp_hosts",
  "imap_hosts",
  "envelope_senders",
  "from_headers",
  "recipient_domains"
] as const satisfies readonly MailProfilePatternKey[];

export const mailProfilePolicyLimitKeys = [
  "allowed_profile_ids",
  "allow_user_profiles",
  "allow_group_profiles",
  "smtp_credentials.inherit",
  "imap_credentials.inherit",
  "whitelist.smtp_hosts",
  "whitelist.imap_hosts",
  "whitelist.envelope_senders",
  "whitelist.from_headers",
  "whitelist.recipient_domains",
  "blacklist.smtp_hosts",
  "blacklist.imap_hosts",
  "blacklist.envelope_senders",
  "blacklist.from_headers",
  "blacklist.recipient_domains"
] as const;

export type MailProfilePolicyLimitKey = typeof mailProfilePolicyLimitKeys[number];
export type MailProfilePolicyLimitPermissions = Partial<Record<MailProfilePolicyLimitKey, boolean>>;

export type MailPolicySourceStep = {
  scope_type: string;
  scope_id?: string | null;
  label: string;
  applied_fields?: string[];
  policy?: MailProfilePolicy | null;
};

export type MailProfilePolicyResponse = {
  scope_type: MailProfileScope;
  scope_id?: string | null;
  policy: MailProfilePolicy;
  effective_policy?: MailProfilePolicy | null;
  parent_policy?: MailProfilePolicy | null;
  effective_policy_sources?: MailPolicySourceStep[];
  parent_policy_sources?: MailPolicySourceStep[];
};

export type MailServerProfilePayload = {
  name: string;
  slug?: string | null;
  description?: string | null;
  is_active?: boolean;
  inherit_to_lower_scopes?: boolean;
  scope_type?: MailProfileScope;
  scope_id?: string | null;
  smtp: MailSmtpTestPayload;
  imap?: MailImapTestPayload | null;
  credentials?: MailServerProfileCredentialsPayload | null;
};

export type MockMailboxMessage = {
  id: string;
  kind: "smtp" | "imap_append" | string;
  created_at: string;
  envelope_from?: string | null;
  envelope_recipients?: string[];
  subject?: string | null;
  from_header?: string | null;
  to_header?: string | null;
  cc_header?: string | null;
  bcc_header?: string | null;
  message_id?: string | null;
  size_bytes?: number;
  body_preview?: string | null;
  attachment_count?: number;
  folder?: string | null;
  raw_eml?: string | null;
  headers?: Record<string, string>;
  attachments?: Array<{ filename?: string | null; content_type?: string | null; size_bytes?: number }>;
};

export type MockMailboxMessageResponse = {
  message: MockMailboxMessage;
};
