import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import type {
  PasswordConfig,
  PasswordValidateResult,
  PasswordValidateRequest,
  SecurityVerifyRequest,
  SecurityStatus,
  PasswordChangeRequest
} from './types';

// ==================== 密码策略管理 ====================

/**
 * 获取密码配置策略
 */
export function getPasswordConfig(): AxiosPromise<PasswordConfig> {
  return request({
    url: '/system/security/password/config',
    method: 'get'
  });
}

/**
 * 密码复杂度验证
 */
export function validatePassword(data: PasswordValidateRequest): AxiosPromise<PasswordValidateResult> {
  return request({
    url: '/system/security/password/validate',
    method: 'post',
    data: data
  });
}

// ==================== 二次验证管理 ====================

/**
 * 发送动态验证码
 * @param authCodeType 验证码类型 1-短信 2-邮件
 */
export function sendAuthCode(authCodeType: number): AxiosPromise<void> {
  return request({
    url: '/system/security/auth/sendCode',
    method: 'post',
    data: { authCodeType }
  });
}

/**
 * 验证动态验证码
 */
export function verifyAuthCode(data: SecurityVerifyRequest): AxiosPromise<void> {
  return request({
    url: '/system/security/auth/verifyCode',
    method: 'post',
    headers: {
      isEncrypt: true
    },
    data: data
  });
}

/**
 * 密码验证 - 用于二次验证
 */
export function verifyPassword(data: SecurityVerifyRequest): AxiosPromise<void> {
  return request({
    url: '/system/security/auth/verifyPassword',
    method: 'post',
    headers: {
      isEncrypt: true
    },
    data: data
  });
}

// ==================== 密码管理 ====================

/**
 * 重置用户密码 (管理员操作)
 */
export function resetUserPassword(data: PasswordChangeRequest): AxiosPromise<void> {
  return request({
    url: '/system/security/password/reset',
    method: 'post',
    headers: {
      isEncrypt: true,
      repeatSubmit: false
    },
    data: data
  });
}

/**
 * 修改当前用户密码
 */
export function changePassword(data: PasswordChangeRequest): AxiosPromise<void> {
  return request({
    url: '/system/security/password/change',
    method: 'post',
    headers: {
      isEncrypt: true,
      repeatSubmit: false
    },
    data: data
  });
}
// ==================== 便捷方法 ====================

/**
 * 获取密码配置（带容错）
 */
export async function getPasswordConfigWithFallback(): Promise<PasswordConfig> {
  try {
    const response = await getPasswordConfig();
    return response.data;
  } catch (error) {

    // 返回默认配置
    return {
      minLength: 8,
      minTypeCount: 3,
      historyCount: 5,
      validityDays: 90,
      maxRetryCount: 5,
      lockDuration: 20,
      strengthLevels: {
        weak: { threshold: 40, label: '弱', color: '#F56C6C' },
        medium: { threshold: 70, label: '中', color: '#E6A23C' },
        strong: { threshold: 100, label: '强', color: '#67C23A' }
      },
      strengthWeights: {
        length: 20,
        uppercase: 15,
        lowercase: 15,
        number: 15,
        specialChar: 15,
        noUsername: 20
      }
    };
  }
}

export default {
  getPasswordConfig,
  getPasswordConfigWithFallback,
  validatePassword,
  sendAuthCode,
  verifyAuthCode,
  verifyPassword,
  resetUserPassword,
  changePassword
};
