import { baseApi } from "@ema-platform/api";
import type {
  Organization,
  Profession,
  ListResponse,
  CreateProfessionPayload,
  UpdateProfessionPayload,
} from "../types/configuration";

const configurationApi = baseApi.injectEndpoints({
  endpoints: (builder) => ({
    getOrganizations: builder.query<ListResponse<Organization>, void>({
      query: () => "/organizations",
      providesTags: ["Api"],
    }),

    getProfessions: builder.query<ListResponse<Profession>, string>({
      query: (params) => ({
        url: `/professions?q=${encodeURIComponent(params)}`,
      }),
      providesTags: ["Api", "backOfficeApi", "ProfessionApi"],
    }),
    createProfession: builder.mutation<Profession, CreateProfessionPayload>({
      query: (body) => ({ url: "/professions", method: "POST", body }),
      invalidatesTags: ["Api"],
    }),
    updateProfession: builder.mutation<Profession, UpdateProfessionPayload>({
      query: ({ id, ...body }) => ({
        url: `/professions/${id}`,
        method: "PUT",
        body,
      }),
      invalidatesTags: ["Api"],
    }),
    deleteProfession: builder.mutation<void, string>({
      query: (id) => ({ url: `/professions/${id}`, method: "DELETE" }),
      invalidatesTags: ["Api"],
    }),
  }),
  overrideExisting: true,
});

export const {
  useGetOrganizationsQuery,
  useGetProfessionsQuery,
  useCreateProfessionMutation,
  useUpdateProfessionMutation,
  useDeleteProfessionMutation,
} = configurationApi;
