import { requestClient } from '#/api/request';

/**
 * 邮件配置接口
 */
export interface EmailConfig {
  // SMTP配置
  smtp_host?: string;
  smtp_port?: number;
  smtp_username?: string;
  smtp_password?: string;
  smtp_from_email?: string;
  smtp_from_name?: string;
  smtp_encryption?: string;
  smtp_enabled?: boolean;
  
  // 邮件模板配置
  register_subject?: string;
  register_content?: string;
  register_enabled?: boolean;
  reset_subject?: string;
  reset_content?: string;
  reset_enabled?: boolean;
  notify_subject?: string;
  notify_content?: string;
  notify_enabled?: boolean;
}

/**
 * 邮件模板预览数据
 */
export interface EmailTemplatePreview {
  subject: string;
  content: string;
}

/**
 * 获取邮件配置
 * @param type 配置类型 smtp | template
 */
export function getEmailConfigApi(type: string) {
  return requestClient.get<EmailConfig>(`/api/common/email/config/${type}`);
}

/**
 * 更新邮件配置
 * @param type 配置类型 smtp | template
 * @param data 配置数据
 */
export function updateEmailConfigApi(type: string, data: Partial<EmailConfig>) {
  return requestClient.put(`/api/common/email/config/${type}`, data);
}

/**
 * 测试邮件发送
 * @param email 收件人邮箱
 * @param type 邮件类型 register | reset | notify
 */
export function testEmailApi(email: string, type: string) {
  return requestClient.post('/api/common/email/test', {
    email,
    type,
  });
}

/**
 * 预览邮件模板
 * @param type 模板类型 register | reset | notify
 */
export function previewEmailTemplateApi(type: string) {
  return requestClient.get<EmailTemplatePreview>(`/api/common/email/template/preview/${type}`);
}

/**
 * 重置邮件配置
 * @param type 配置类型 smtp | template
 */
export function resetEmailConfigApi(type: string) {
  return requestClient.post(`/api/common/email/config/reset/${type}`);
}

/**
 * 获取邮件发送统计
 */
export function getEmailStatsApi() {
  return requestClient.get('/api/common/email/stats');
}

/**
 * 获取邮件发送日志
 * @param params 查询参数
 */
export function getEmailLogsApi(params?: {
  page?: number;
  limit?: number;
  type?: string;
  status?: string;
  start_date?: string;
  end_date?: string;
}) {
  return requestClient.get('/api/common/email/logs', { params });
}

/**
 * 清空邮件发送队列
 */
export function clearEmailQueueApi() {
  return requestClient.post('/api/common/email/queue/clear');
}

/**
 * 重试失败的邮件
 * @param id 邮件ID
 */
export function retryEmailApi(id: number) {
  return requestClient.post(`/api/common/email/retry/${id}`);
}

/**
 * 批量重试失败的邮件
 * @param ids 邮件ID数组
 */
export function batchRetryEmailApi(ids: number[]) {
  return requestClient.post('/api/common/email/batch-retry', { ids });
}

/**
 * 删除邮件日志
 * @param id 邮件ID
 */
export function deleteEmailLogApi(id: number) {
  return requestClient.delete(`/api/common/email/log/${id}`);
}

/**
 * 批量删除邮件日志
 * @param ids 邮件ID数组
 */
export function batchDeleteEmailLogApi(ids: number[]) {
  return requestClient.delete('/api/common/email/logs', { data: { ids } });
}