import type { ApiSettings } from "../types"; import { apiFetch } from "./client"; export type MailSecurity = "plain" | "tls" | "starttls"; export type MailProfileScope = "system" | "tenant" | "user" | "group" | "campaign"; export type MailSmtpTestPayload = { host?: string | null; port?: number | null; username?: string | null; password?: string | null; security?: MailSecurity; timeout_seconds?: number; }; export type MailImapTestPayload = MailSmtpTestPayload & { sent_folder?: string | null; }; export type MailTransportCredentialsPayload = { username?: string | null; password?: string | null; }; export type MailServerProfileCredentialsPayload = { smtp?: MailTransportCredentialsPayload; imap?: MailTransportCredentialsPayload; }; export type MailConnectionTestResponse = { ok: boolean; protocol: "smtp" | "imap"; host?: string | null; port?: number | null; security?: MailSecurity | string | null; message: string; details?: Record; }; export type MailImapFolderResponse = { name: string; flags?: string[]; }; 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; details?: Record; }; export type MailServerProfile = { id: string; tenant_id?: string | null; scope_type: MailProfileScope; scope_id?: string | null; name: string; slug: string; description?: string | null; is_active: boolean; smtp: MailSmtpTestPayload; imap?: MailImapTestPayload | null; credentials?: MailServerProfileCredentialsPayload | null; smtp_password_configured: boolean; imap_password_configured: boolean; created_at: string; updated_at: string; }; export type MailServerProfileListResponse = { profiles: MailServerProfile[] }; export const mailProfilePatternKeys = [ "smtp_hosts", "imap_hosts", "envelope_senders", "from_headers", "recipient_domains" ] as const; export type MailProfilePatternKey = typeof mailProfilePatternKeys[number]; export type MailProfilePatternRules = Partial>; export type MailCredentialPolicy = { inherit?: boolean | null; allow_override?: boolean | null; }; export type MailProfilePolicy = { allowed_profile_ids?: string[] | null; allow_user_profiles?: boolean | null; allow_group_profiles?: boolean | null; allow_campaign_profiles?: boolean | null; smtp_credentials?: MailCredentialPolicy | null; imap_credentials?: MailCredentialPolicy | null; whitelist?: MailProfilePatternRules | null; blacklist?: MailProfilePatternRules | null; }; export type PolicySourceStep = { scope_type: string; scope_id?: string | null; label: string; applied_fields?: string[]; }; export type MailProfilePolicyResponse = { scope_type: MailProfileScope; scope_id?: string | null; policy: MailProfilePolicy; effective_policy?: MailProfilePolicy | null; parent_policy?: MailProfilePolicy | null; effective_policy_sources?: PolicySourceStep[]; parent_policy_sources?: PolicySourceStep[]; }; export type MailServerProfilePayload = { name: string; slug?: string | null; description?: string | null; is_active?: boolean; scope_type?: MailProfileScope; scope_id?: string | null; smtp: MailSmtpTestPayload; imap?: MailImapTestPayload | null; credentials?: MailServerProfileCredentialsPayload | null; }; export async function listMailServerProfiles(settings: ApiSettings, includeInactive = false, campaignId?: string): Promise { const params = new URLSearchParams(); if (includeInactive) params.set("include_inactive", "true"); if (campaignId) params.set("campaign_id", campaignId); const suffix = params.toString() ? `?${params.toString()}` : ""; const response = await apiFetch(settings, `/api/v1/mail/profiles${suffix}`); return response.profiles ?? []; } export async function createMailServerProfile(settings: ApiSettings, payload: MailServerProfilePayload): Promise { return apiFetch(settings, "/api/v1/mail/profiles", { method: "POST", body: JSON.stringify(payload) }); } export async function getMailProfilePolicy( settings: ApiSettings, scopeType: MailProfileScope, scopeId?: string | null, campaignId?: string | null ): Promise { const params = new URLSearchParams(); if (scopeId) params.set("scope_id", scopeId); if (campaignId) params.set("campaign_id", campaignId); const suffix = params.toString() ? `?${params.toString()}` : ""; return apiFetch(settings, `/api/v1/mail/policies/${encodeURIComponent(scopeType)}${suffix}`); } export async function testMailProfileSmtp(settings: ApiSettings, profileId: string): Promise { return apiFetch(settings, `/api/v1/mail/profiles/${encodeURIComponent(profileId)}/test-smtp`, { method: "POST" }); } export async function testMailProfileImap(settings: ApiSettings, profileId: string): Promise { return apiFetch(settings, `/api/v1/mail/profiles/${encodeURIComponent(profileId)}/test-imap`, { method: "POST" }); } export async function listMailProfileImapFolders(settings: ApiSettings, profileId: string): Promise { return apiFetch(settings, `/api/v1/mail/profiles/${encodeURIComponent(profileId)}/list-imap-folders`, { method: "POST" }); } export async function testSmtpSettings(settings: ApiSettings, payload: MailSmtpTestPayload): Promise { return apiFetch(settings, "/api/v1/mail/test-smtp", { method: "POST", body: JSON.stringify(payload) }); } export async function testImapSettings(settings: ApiSettings, payload: MailImapTestPayload): Promise { return apiFetch(settings, "/api/v1/mail/test-imap", { method: "POST", body: JSON.stringify(payload) }); } export async function listImapFolders(settings: ApiSettings, payload: MailImapTestPayload): Promise { return apiFetch(settings, "/api/v1/mail/list-imap-folders", { method: "POST", body: JSON.stringify(payload) }); } 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; attachments?: Array<{ filename?: string | null; content_type?: string | null; size_bytes?: number }>; }; export type MockMailboxMessageResponse = { message: MockMailboxMessage; }; export async function getMockMailboxMessage(settings: ApiSettings, id: string): Promise { return apiFetch(settings, `/api/v1/dev/mailbox/messages/${encodeURIComponent(id)}`); }