import { baseApi } from '@ema-platform/api';
import type {
  Certification,
  ListResponse,
  CreateCertificationPayload,
  UpdateCertificationPayload,
} from '../types/certification';

const certificationApi = baseApi.injectEndpoints({
  endpoints: (builder) => ({
    getCertifications: builder.query<ListResponse<Certification>, void>({
      query: () => '/certifications',
      providesTags: ['Api'],
    }),
    getCertification: builder.query<Certification, string>({
      query: (id) => `/certifications/${id}`,
      providesTags: ['Api'],
    }),
    createCertification: builder.mutation<Certification, CreateCertificationPayload>({
      query: (body) => ({ url: '/certifications', method: 'POST', body }),
      invalidatesTags: ['Api'],
    }),
    updateCertification: builder.mutation<Certification, UpdateCertificationPayload>({
      query: ({ id, ...body }) => ({
        url: `/certifications/${id}`,
        method: 'PUT',
        body,
      }),
      invalidatesTags: ['Api'],
    }),
    deleteCertification: builder.mutation<void, string>({
      query: (id) => ({ url: `/certifications/${id}`, method: 'DELETE' }),
      invalidatesTags: ['Api'],
    }),
  }),
  overrideExisting: false,
});

export const {
  useGetCertificationsQuery,
  useGetCertificationQuery,
  useCreateCertificationMutation,
  useUpdateCertificationMutation,
  useDeleteCertificationMutation,
} = certificationApi;
