import apiClient from './client'
import type {
  ConnectionTestResult,
  MailboxCreateRequest,
  MailboxListResponse,
  MailboxResponse,
  MailboxUpdateRequest,
} from '@/types/api.types'

export const mailboxesApi = {
  list: (page = 1, page_size = 50) =>
    apiClient
      .get<MailboxListResponse>('/mailboxes', { params: { page, page_size } })
      .then((r) => r.data),

  get: (id: string) =>
    apiClient.get<MailboxResponse>(`/mailboxes/${id}`).then((r) => r.data),

  create: (data: MailboxCreateRequest) =>
    apiClient.post<MailboxResponse>('/mailboxes', data).then((r) => r.data),

  update: (id: string, data: MailboxUpdateRequest) =>
    apiClient.put<MailboxResponse>(`/mailboxes/${id}`, data).then((r) => r.data),

  delete: (id: string) => apiClient.delete(`/mailboxes/${id}`),

  testConnection: (id: string, protocol: 'imap' | 'smtp' = 'imap') =>
    apiClient
      .post<ConnectionTestResult>(`/mailboxes/${id}/test-connection`, { protocol })
      .then((r) => r.data),
}
