import { baseApi } from '@ema-platform/api';
import type { AddressPayload } from '../types/address';

const addressApi = baseApi
  .enhanceEndpoints({ addTagTypes: ['CurrentProfile'] as const })
  .injectEndpoints({
    endpoints: (builder) => ({
      /** The profile-address endpoint creates or updates through POST. */
      saveMyAddress: builder.mutation<unknown, { profileId: string; body: AddressPayload }>({
        query: ({ profileId, body }) => ({
          url: `/addresss/profile/${profileId}`,
          method: 'POST',
          body,
        }),
        invalidatesTags: (_result, error) => (error ? [] : ['CurrentProfile']),
      }),
    }),
    overrideExisting: false,
  });

export const { useSaveMyAddressMutation } = addressApi;
