import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import { getConfigValue } from '@brojs/cli';

import { Master } from '../../models/api/master';

import { extractBodyFromResponse } from './utils';

export const api = createApi({
  reducerPath: 'api',
  baseQuery: fetchBaseQuery({ baseUrl: getConfigValue('dry-wash.api') }),
  tagTypes: ['Masters'],
  endpoints: (builder) => ({
    getMasters: builder.query<Master[], void>({
      query: () => ({ url: '/arm/masters' }),
      transformResponse: extractBodyFromResponse<Master[]>,
      providesTags: ['Masters'],
    }),
    addMaster: builder.mutation<void, Pick<Master, 'name' | 'phone'>>({
      query: (master) => ({
        url: '/arm/masters',
        method: 'POST',
        body: master,
      }),
      invalidatesTags: ['Masters'],
    }),
  }),
});
