import { baseApi } from "@ema-platform/api";
import type {
  Organization,
  Profession,
  ListResponse,
  CreateProfessionPayload,
  UpdateProfessionPayload,
  NumberFormatConfig,
  NumberFormatPayload,
} 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"],
    }),

    // Number formats — the shape of generated seafarer, seaman book and BTC
    // identifiers. The counter behind each stays server-side; only the
    // rendering is configurable here.
    getNumberFormats: builder.query<NumberFormatConfig[], void>({
      query: () => "/number-format-configs",
      providesTags: ["Api", "NumberFormatApi"],
    }),
    createNumberFormat: builder.mutation<
      NumberFormatConfig,
      NumberFormatPayload
    >({
      query: (body) => ({
        url: "/number-format-configs",
        method: "POST",
        body,
      }),
      invalidatesTags: ["Api", "NumberFormatApi"],
    }),
    updateNumberFormat: builder.mutation<
      NumberFormatConfig,
      { id: string } & Partial<NumberFormatPayload>
    >({
      query: ({ id, ...body }) => ({
        url: `/number-format-configs/${id}`,
        method: "PATCH",
        body,
      }),
      invalidatesTags: ["Api", "NumberFormatApi"],
    }),
    /**
     * Server-rendered sample. Asked of the server rather than formatted in the
     * browser so the preview cannot drift from what approval will actually
     * generate — the two would be the same rule written twice.
     */
    previewNumberFormat: builder.mutation<
      { sample: string },
      NumberFormatPayload
    >({
      query: (body) => ({
        url: "/number-format-configs/preview",
        method: "POST",
        body,
      }),
    }),
  }),
  overrideExisting: true,
});

export const {
  useGetOrganizationsQuery,
  useGetProfessionsQuery,
  useCreateProfessionMutation,
  useUpdateProfessionMutation,
  useDeleteProfessionMutation,
  useGetNumberFormatsQuery,
  useCreateNumberFormatMutation,
  useUpdateNumberFormatMutation,
  usePreviewNumberFormatMutation,
} = configurationApi;
