// admin-api.service.ts

import type { DeepPartial } from "react-hook-form";
import { BaseApiService } from "./core-api-service";
import { mapTableStateToPaginationQueryDSL } from "~/features/table";
import axios from "~/lib/axios";

class ResourceApiService extends BaseApiService<IResource> {
  constructor() {
    super("resources");
  }

  async resourceByRole(
    role_id: IRole["id"],
    values?: DeepPartial<TableState<IResource>> | undefined
  ) {
    const params = values
      ? mapTableStateToPaginationQueryDSL(values as TableState<IResource>)
      : {};

    const response = await axios({
      url: `${this.resourceUrl}/role/${role_id}`,
      params: { ...params },
      withCredentials: true,
      method: "GET",
    });

    return response.data;
  }

  async updateResourceRoles(role_id: IRole["id"], values: number[]) {
    const response = await axios({
      url: `${this.resourceUrl}/role/${role_id}`,
      withCredentials: true,
      data: {
        resource_ids: values,
        role_id,
      },
      method: "PUT",
    });

    return response.data;
  }
}

export const resourceApi = new ResourceApiService();
