import { http } from "@/utils/http";

// 后端返回的登录结果格式
export type UserResult = {
  code?: number;
  msg?: string;
  id?: number;
  accessToken?: string;
  success?: boolean;
};

export type RefreshTokenResult = {
  success: boolean;
  data: {
    accessToken: string;
    refreshToken: string;
    expires: Date;
  };
};

export type UserInfo = {
  avatar: string;
  username: string;
  nickname: string;
  email: string;
  phone: string;
  description: string;
};

export type UserInfoResult = {
  success: boolean;
  data: UserInfo;
};

type ResultTable = {
  success: boolean;
  data?: {
    list: Array<any>;
    total?: number;
    pageSize?: number;
    currentPage?: number;
  };
};

/** 登录 - 对接实际后端接口 */
export const getLogin = (data?: object) => {
  return http.request<UserResult>("post", "/admin-user/login", { data });
};

/** 获取用户信息 */
export const getProfile = () => {
  return http.request<any>("get", "/admin-user/profile");
};

/** 获取字典数据 */
export const getDictList = () => {
  return http.request<any>("post", "/dataDict/list", { data: {} });
};

/** 刷新登录状态 */
export const refreshLogin = () => {
  return http.request<any>("get", "/admin-user/refresh");
};

/** 刷新`token` */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", "/refresh-token", { data });
};

/** 登出 */
export const logout = () => {
  return http.request<any>("post", "/admin-user/logout");
};

/** 获取当前用户信息（用于个人设置页面） */
export const getMine = () => {
  return http.request<UserInfoResult>("get", "/admin-user/profile");
};

/** 获取当前用户的安全日志 */
export const getMineLogs = () => {
  return http.request<ResultTable>("get", "/admin-user/logs");
};
