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

// 基础设置接口
export interface BasicSettings {
  site_name: string;
  site_title: string;
  site_keywords: string;
  site_description: string;
  site_logo: string;
  site_icon: string;
  site_icp: string;
  site_copyright: string;
  site_status: boolean;
  site_close_reason: string;
}

// 邮件设置接口
export interface EmailSettings {
  enabled: boolean;
  driver: 'mailgun' | 'sendmail' | 'ses' | 'smtp';
  smtp_host: string;
  smtp_port: number;
  smtp_username: string;
  smtp_password: string;
  smtp_encryption: 'none' | 'ssl' | 'tls';
  from_email: string;
  from_name: string;
}

// 短信设置接口
export interface SmsSettings {
  enabled: boolean;
  driver: 'aliyun' | 'huawei' | 'qiniu' | 'tencent';
  access_key_id: string;
  access_key_secret: string;
  sign_name: string;
  template_code: string;
  code_length: number;
  code_expire: number;
  rate_limit: number;
  test_mobile: string;
}

// 存储设置接口
export interface StorageSettings {
  default_driver: 'aliyun_oss' | 'aws_s3' | 'local' | 'qiniu' | 'tencent_cos';
  max_file_size: number;
  allowed_extensions: string[];
  local: {
    base_url: string;
    storage_path: string;
  };
  aliyun_oss: {
    access_key_id: string;
    access_key_secret: string;
    bucket: string;
    custom_domain?: string;
    endpoint: string;
  };
  tencent_cos: {
    bucket: string;
    custom_domain?: string;
    region: string;
    secret_id: string;
    secret_key: string;
  };
  qiniu: {
    access_key: string;
    bucket: string;
    domain: string;
    secret_key: string;
  };
  aws_s3: {
    access_key_id: string;
    bucket: string;
    custom_domain?: string;
    region: string;
    secret_access_key: string;
  };
}

// 支付设置接口
export interface PaymentSettings {
  alipay: {
    app_id: string;
    enabled: boolean;
    mode: 'production' | 'sandbox';
    notify_url?: string;
    private_key: string;
    public_key: string;
    return_url?: string;
  };
  wechat: {
    api_key: string;
    app_id: string;
    cert_path?: string;
    enabled: boolean;
    key_path?: string;
    mch_id: string;
    mode: 'production' | 'sandbox';
    notify_url?: string;
  };
  unionpay: {
    cert_password: string;
    cert_path: string;
    enabled: boolean;
    mer_id: string;
    mode: 'production' | 'sandbox';
    notify_url?: string;
    return_url?: string;
  };
}

// 登录设置接口
export interface LoginSettings {
  login_methods: {
    email: boolean;
    github: boolean;
    mobile: boolean;
    qq: boolean;
    username: boolean;
    wechat: boolean;
  };
  captcha: {
    enabled: boolean;
    expire: number;
    length: number;
    type: 'email' | 'image' | 'sms';
  };
  password_policy: {
    min_length: number;
    require_lowercase: boolean;
    require_numbers: boolean;
    require_symbols: boolean;
    require_uppercase: boolean;
  };
  session: {
    max_sessions: number;
    remember_me: boolean;
    timeout: number;
  };
  security: {
    force_logout_on_password_change: boolean;
    lockout_duration: number;
    max_login_attempts: number;
  };
}

// API 接口定义

// 基础设置
export const getBasicSettingsApi = () => {
  return requestClient.get<BasicSettings>('/api/system/settings/basic');
};

export const updateBasicSettingsApi = (data: Partial<BasicSettings>) => {
  return requestClient.put<BasicSettings>('/api/system/settings/basic', data);
};

// 邮件设置
export const getEmailSettingsApi = () => {
  return requestClient.get<EmailSettings>('/api/system/settings/email');
};

export const updateEmailSettingsApi = (data: Partial<EmailSettings>) => {
  return requestClient.put<EmailSettings>('/api/system/settings/email', data);
};

export const testEmailApi = (email: string) => {
  return requestClient.post('/api/system/settings/email/test', { email });
};

// 短信设置
export const getSmsSettingsApi = () => {
  return requestClient.get<SmsSettings>('/api/system/settings/sms');
};

export const updateSmsSettingsApi = (data: Partial<SmsSettings>) => {
  return requestClient.put<SmsSettings>('/api/system/settings/sms', data);
};

export const testSmsApi = (mobile: string) => {
  return requestClient.post('/api/system/settings/sms/test', { mobile });
};

// 存储设置
export const getStorageSettingsApi = () => {
  return requestClient.get<StorageSettings>('/api/system/settings/storage');
};

export const updateStorageSettingsApi = (data: Partial<StorageSettings>) => {
  return requestClient.put<StorageSettings>(
    '/api/system/settings/storage',
    data,
  );
};

export const testStorageConnectionApi = (driver: string) => {
  return requestClient.post('/api/system/settings/storage/test', { driver });
};

// 支付设置
export const getPaymentSettingsApi = () => {
  return requestClient.get<PaymentSettings>('/api/system/settings/payment');
};

export const updatePaymentSettingsApi = (data: Partial<PaymentSettings>) => {
  return requestClient.put<PaymentSettings>(
    '/api/system/settings/payment',
    data,
  );
};

// 登录设置
export const getLoginSettingsApi = () => {
  return requestClient.get<LoginSettings>('/api/system/settings/login');
};

export const updateLoginSettingsApi = (data: Partial<LoginSettings>) => {
  return requestClient.put<LoginSettings>('/api/system/settings/login', data);
};

// 获取所有设置
export const getAllSettingsApi = () => {
  return requestClient.get<{
    basic: BasicSettings;
    email: EmailSettings;
    login: LoginSettings;
    payment: PaymentSettings;
    sms: SmsSettings;
    storage: StorageSettings;
  }>('/api/system/settings');
};

// 重置设置到默认值
export const resetSettingsApi = (
  type: 'basic' | 'email' | 'login' | 'payment' | 'sms' | 'storage',
) => {
  return requestClient.post(`/api/system/settings/${type}/reset`);
};

// 导出设置
export const exportSettingsApi = () => {
  return requestClient.get('/api/system/settings/export', {
    responseType: 'blob',
  });
};

// 导入设置
export const importSettingsApi = (file: File) => {
  const formData = new FormData();
  formData.append('file', file);
  return requestClient.post('/api/system/settings/import', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
};

// 通知设置接口
export interface NotificationSettings {
  email: {
    enabled: boolean;
    types: string[];
    adminEmails: string[];
    rateLimit: number;
    retryTimes: number;
    queueDelay: number;
  };
  sms: {
    enabled: boolean;
    types: string[];
    adminPhones: string[];
    rateLimit: number;
    retryTimes: number;
    queueDelay: number;
  };
  system: {
    enabled: boolean;
    types: string[];
    retentionDays: number;
    maxPerUser: number;
    autoRead: boolean;
  };
  wechat: {
    enabled: boolean;
    types: string[];
    templateIds: Record<string, string>;
    retryTimes: number;
    queueDelay: number;
  };
}

export interface UpdateNotificationSettingsParams {
  type: 'email' | 'sms' | 'system' | 'wechat';
  settings: Partial<NotificationSettings[keyof NotificationSettings]>;
}

// 通知设置API
export const getNotificationSettingsApi = () => {
  return requestClient.get<NotificationSettings>('/api/system/settings/notification');
};

export const updateNotificationSettingsApi = (data: UpdateNotificationSettingsParams) => {
  return requestClient.put<NotificationSettings>('/api/system/settings/notification', data);
};

export const resetNotificationSettingsApi = (type: 'email' | 'sms' | 'system' | 'wechat') => {
  return requestClient.post(`/api/system/settings/notification/${type}/reset`);
};

export const testNotificationApi = (type: 'email' | 'sms' | 'system' | 'wechat') => {
  return requestClient.post(`/api/system/settings/notification/${type}/test`);
};
