import type { UserInfo } from "@/utils/auth";
import { http } from "@/utils/http";

export type LoginResult = {
  /** `token` */
  accessToken: string;
  /** 用于调用刷新`accessToken`的接口时所需的`token` */
  refreshToken: string;
  /** `accessToken`的过期时间戳（毫秒） */
  expiresTime: number;
};

/**
 * 登录
 * @param data
 * @returns
 */
export const getLogin = (data?: object) => {
  return http.request<LoginResult>("post", "/api/login", {
    headers: {
      "content-type": "application/x-www-form-urlencoded"
    },
    data
  });
};
/** 刷新token */
export const refreshTokenApi = (data: { refreshToken: string }) => {
  return http.request<LoginResult>("post", "/api/refreshToken", {
    headers: {
      "content-type": "application/x-www-form-urlencoded"
    },
    data
  });
};

export type CaptchaResponse = {
  /**验证码ID */
  uuid: string | null;
  /**验证码 */
  captcha: string | null;
  /**是否开启验证码 */
  captcha_enabled: boolean;
  /**是否开启注册 */
  register_enabled: boolean;
};

/** 获取验证码 */
export const GetCaptchaAPI = () => {
  return http.request<CaptchaResponse>("get", "/api/captcha");
};

/**
 * 获取用户动态路由
 * @returns
 */
export const getUserRoutesAPI = () => {
  return http.request<any[]>("GET", "/api/getRoutes");
};

/**
 * 获取用户信息
 */
export const getUserInfoAPI = () => {
  return http.request<UserInfo>("get", `/api/info`);
};

/**
 * 退出登录
 */
export const logoutAPI = () => {
  return http.request<null>("post", `/api/logout`);
};

/**获取验证码参数 */
type GetCodeParams = {
  /**用户账号 */
  username: string;
  /**验证码类型 */
  title: string;
  /**收件邮箱 */
  mail: string;
};

/**
 * 获取验证码
 * @param data
 * @returns
 */
export const postGetCodeAPI = (data: GetCodeParams) => {
  return http.request<null>("post", `/api/code`, {
    data
  });
};

/**
 * 注册参数
 */
type RegisterParams = {
  /**用户名 */
  username: string;
  /**密码 */
  password: string;
  /**邮箱 */
  email: string;
  /**验证码 */
  code: string;
  /**性别 */
  gender: number;
  /**昵称 */
  nickname: string;
  /**手机号 */
  phone: string;
  /**部门ID */
  department_id: string;
};

/**
 * 用户注册
 * @param data
 * @returns
 */
export const postRegisterAPI = (data: RegisterParams) => {
  return http.request<null>("post", `/api/register`, {
    data
  });
};

/**
 * 重置密码
 */
type ResetPasswordParams = {
  /**
   * 用户账号
   */
  username: string;
  /**
   * 邮箱
   */
  mail: string;
  /**
   * 验证码
   */
  code: string;
  /**
   * 密码
   */
  password: string;
};
/**
 * 重置密码
 * @param data
 * @returns
 */
export const postResetPasswordAPI = (data: ResetPasswordParams) => {
  return http.request<null>("post", `/api/resetPassword`, {
    data
  });
};
