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

// 短信配置接口
export interface SmsConfig {
  provider: 'aliyun' | 'tencent';
  access_key_id?: string;
  access_key_secret?: string;
  sign_name?: string;
  region?: string;
  secret_id?: string;
  secret_key?: string;
  app_id?: string;
  sign_content?: string;
  enabled: boolean;
  debug_mode: boolean;
  templates: SmsTemplate[];
  rate_limit: {
    per_minute: number;
    per_hour: number;
    per_day: number;
  };
}

// 短信模板接口
export interface SmsTemplate {
  type: 'verify_code' | 'notification' | 'marketing';
  template_id: string;
  content: string;
  variables?: string;
}

// 短信统计接口
export interface SmsStats {
  total_sent: number;
  success_count: number;
  failed_count: number;
  today_sent: number;
  this_month_sent: number;
}

// 短信日志接口
export interface SmsLog {
  id: number;
  mobile: string;
  content: string;
  template_type: string;
  status: 'pending' | 'sent' | 'failed';
  error_message?: string;
  provider: string;
  created_at: string;
  sent_at?: string;
}

/**
 * 获取短信配置
 */
export async function getSmsConfigApi(): Promise<SmsConfig> {
  return requestClient.get('/api/admin/settings/sms');
}

/**
 * 更新短信配置
 */
export async function updateSmsConfigApi(data: SmsConfig): Promise<void> {
  return requestClient.put('/api/admin/settings/sms', data);
}

/**
 * 测试短信发送
 */
export async function testSmsApi(data: {
  mobile: string;
  template_type: string;
  content: string;
}): Promise<void> {
  return requestClient.post('/api/admin/settings/sms/test', data);
}

/**
 * 重置短信配置
 */
export async function resetSmsConfigApi(): Promise<void> {
  return requestClient.post('/api/admin/settings/sms/reset');
}

/**
 * 获取短信统计
 */
export async function getSmsStatsApi(): Promise<SmsStats> {
  return requestClient.get('/api/admin/settings/sms/stats');
}

/**
 * 获取短信日志
 */
export async function getSmsLogsApi(params?: {
  page?: number;
  limit?: number;
  mobile?: string;
  status?: string;
  start_date?: string;
  end_date?: string;
}): Promise<{
  data: SmsLog[];
  total: number;
  page: number;
  limit: number;
}> {
  return requestClient.get('/api/admin/settings/sms/logs', { params });
}

/**
 * 清空短信队列
 */
export async function clearSmsQueueApi(): Promise<void> {
  return requestClient.post('/api/admin/settings/sms/clear-queue');
}

/**
 * 重试发送短信
 */
export async function retrySmsApi(id: number): Promise<void> {
  return requestClient.post(`/api/admin/settings/sms/retry/${id}`);
}

/**
 * 批量重试发送短信
 */
export async function batchRetrySmsApi(ids: number[]): Promise<void> {
  return requestClient.post('/api/admin/settings/sms/batch-retry', { ids });
}

/**
 * 删除短信日志
 */
export async function deleteSmsLogApi(id: number): Promise<void> {
  return requestClient.delete(`/api/admin/settings/sms/logs/${id}`);
}

/**
 * 批量删除短信日志
 */
export async function batchDeleteSmsLogApi(ids: number[]): Promise<void> {
  return requestClient.post('/api/admin/settings/sms/logs/batch-delete', { ids });
}

/**
 * 获取短信模板预览
 */
export async function previewSmsTemplateApi(data: {
  template_id: string;
  variables: Record<string, any>;
}): Promise<{
  content: string;
  preview: string;
}> {
  return requestClient.post('/api/admin/settings/sms/template/preview', data);
}

/**
 * 验证短信配置
 */
export async function validateSmsConfigApi(data: Partial<SmsConfig>): Promise<{
  valid: boolean;
  errors: string[];
}> {
  return requestClient.post('/api/admin/settings/sms/validate', data);
}

/**
 * 获取短信服务商配置模板
 */
export async function getSmsProviderTemplateApi(provider: string): Promise<{
  fields: Array<{
    key: string;
    label: string;
    type: string;
    required: boolean;
    placeholder?: string;
    options?: Array<{ label: string; value: string }>;
  }>;
}> {
  return requestClient.get(`/api/admin/settings/sms/provider/${provider}/template`);
}