import { baseRequestClient, requestClient } from '#/api/request';

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    password?: string;
    username?: string;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    accessToken: string;
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
  }
  export interface DeptResult {
    id: number;
    name: string;
  }
  export interface RoleResult {
    id: number;
    name: string;
  }
  export interface CsrfTokenResult {
    token: string;
    headerName: string;
    parameterName: string;
  }

  export interface UserInfo {
    id?: number;
    username: string;
    dept: DeptResult;
    role: RoleResult;
    permissions: string[];
  }
  export interface UserResult {
    user: string | UserInfo;
    csrf: CsrfTokenResult;
  }
}

/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  return requestClient.post<AuthApi.UserResult>('/rest/user/login', data);
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
  return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
    withCredentials: true,
  });
}

/**
 * 退出登录
 */
export async function logoutApi() {
  return baseRequestClient.post('/rest/user/logout', {
    withCredentials: true,
  });
}

/**
 * 获取用户权限码
 */
export async function getAccessCodesApi() {
  return requestClient.get<string[]>('/auth/codes');
}
