import type {
  BasicFetchResult,
  BasicPageParams,
  RelationRequest,
  StatusParams,
} from '#/api/model';

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

export namespace RoleApi {
  export interface PageParams extends BasicPageParams {
    code?: string;
    name?: string;
  }

  export interface BasicRecordItem {
    code: string;
    name: string;
  }

  export interface RecordItem extends BasicRecordItem {
    id: number;
  }

  export type PageResult = BasicFetchResult<RecordItem>;

  export interface StatisticsItem {
    id: number;
    name: string;
    count: number;
  }

  export const getPage = (params: PageParams) =>
    requestClient.get<PageResult>('/role/page', { params });

  export const getDetail = (id: number) =>
    requestClient.get<RecordItem>('/role/entity', {
      params: { id },
    });

  export const addDetail = (data: BasicRecordItem) =>
    requestClient.post('/role', data);

  export const editDetail = (data: RecordItem) =>
    requestClient.put('/role', data);

  export const deleteDetail = (id: number) =>
    requestClient.delete('/role', { data: { id } });
  export const updateStatus = (data: StatusParams) =>
    requestClient.put('/role/status', data);

  export const getMenuIds = (id: number) =>
    requestClient.get<number[]>('/role/menu-ids', { params: { id } });
  export const getUserIds = (id: number) =>
    requestClient.get<number[]>('/role/user-ids', { params: { id } });

  export const updateGrant = (data: RelationRequest) =>
    requestClient.post('/role/grant', data);
  export const updateGrantUser = (data: RelationRequest) =>
    requestClient.post('/role/grant-user', data);

  export const getStatistics = () =>
    requestClient.get<StatisticsItem[]>('/role/statistics');
}
