/**
 * @description 认证相关API
 * @author yslg
 * @since 2025-10-06
 */

import { api } from '@/apis/index';
import type { LoginParam, LoginDomain, ResultDomain } from '@/types';

/**
 * 认证API服务
 */
export const authApi = {
  /**
   * 用户登录
   * @param loginParam 登录参数
   * @returns Promise<ResultDomain<LoginDomain>>
   */
  async login(loginParam: LoginParam): Promise<ResultDomain<LoginDomain>> {
    const response = await api.post<LoginDomain>('/auth/login', loginParam);
    return response.data;
  },

  /**
   * 用户登出
   * @returns Promise<ResultDomain<string>>
   */
  async logout(): Promise<ResultDomain<string>> {
    const response = await api.post<string>('/auth/logout');
    return response.data;
  },

  /**
   * 获取验证码
   * @returns Promise<ResultDomain<{captchaId: string, captchaImage: string}>>
   */
  async getCaptcha(): Promise<ResultDomain<{ captchaId: string; captchaImage: string }>> {
    const response = await api.get<{ captchaId: string; captchaImage: string }>('/auth/captcha');
    return response.data;
  },

  /**
   * 刷新Token
   * @returns Promise<ResultDomain<string>>
   */
  async refreshToken(): Promise<ResultDomain<string>> {
    const response = await api.post<string>('/auth/refresh-token');
    return response.data;
  },

  /**
   * 发送手机验证码
   * @param phone 手机号
   * @returns Promise<ResultDomain<boolean>>
   */
  async sendSmsCode(phone: string): Promise<ResultDomain<boolean>> {
    const response = await api.post<boolean>('/auth/send-sms-code', { phone });
    return response.data;
  },

  /**
   * 发送邮箱验证码
   * @param email 邮箱
   * @returns Promise<ResultDomain<boolean>>
   */
  async sendEmailCode(email: string): Promise<ResultDomain<boolean>> {
    const response = await api.post<boolean>('/auth/send-email-code', { email });
    return response.data;
  }
};

