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

type Result = {
  code: number;
  message: string;
  data?: any;
};

type ResultTable = {
  code: number;
  message: string;
  data?: {
    list: Array<any>;
    total: number;
    pageSize: number;
    currentPage: number;
  };
};

export type UserResult = {
  code: number;
  message: string;
  data: {
    avatar: string;
    username: string;
    nickname: string;
    roles: Array<string>;
    permissions: Array<string>;
  };
};

export type RefreshTokenResult = {
  code: number;
  message: string;
  data: {
    accessToken: string;
    refreshToken?: string;
    expires?: Date;
  };
};

export const getLogin = (data: { username: string; password: string }) => {
  return http.request<UserResult>("post", "/login", { data });
};

export const refreshTokenApi = (data: { refreshToken: string }) => {
  return http.request<RefreshTokenResult>("post", "/login/refresh-token", { data });
};

export const getUserList = (params: {
  page?: number;
  pageSize?: number;
  phone?: string;
  nickname?: string;
  status?: number;
}) => {
  return http.request<ResultTable>("get", "/users/list", { params });
};

export const getUserById = (id: number) => {
  return http.request<Result>("get", `/users/${id}`);
};

export const getUserStatistics = () => {
  return http.request<Result>("get", "/users/statistics");
};

export const updateUserStatus = (id: number, data: { status: number }) => {
  return http.request<Result>("put", `/users/${id}/status`, { data });
};

export const updateUserCreditScore = (
  id: number,
  data: { creditScore: number }
) => {
  return http.request<Result>("put", `/users/${id}/credit`, { data });
};

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

export const getMine = () => {
  return http.request<Result>("get", "/admin/mine");
};

export const getMineLogs = (params: { page?: number; pageSize?: number }) => {
  return http.request<ResultTable>("get", "/admin/logs", { params });
};
