import { useMutation, useQueryClient } from "@tanstack/react-query";

import { api } from "@/services/api";
import type {
  CreateDropdownOptionDto,
  CreateDropdownSettingDto,
  UpdateDropdownOptionDto,
  UpdateDropdownSettingDto,
} from "@/types/dropdownSettings";

/* ----------------------------- Mutations ----------------------------- */

export const useCreateDropdownSetting = () => {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (dto: CreateDropdownSettingDto) =>
      api.dropdownSettings.create.call(dto),
    onSuccess: () =>
      qc.invalidateQueries({ queryKey: api.dropdownSettings.list.queryKey() }),
  });
};

export const useUpdateDropdownSetting = () => {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      id,
      dto,
    }: {
      id: string;
      dto: UpdateDropdownSettingDto;
    }) => api.dropdownSettings.update.call({ id, dto }),
    onSuccess: (_data, { id }) => {
      qc.invalidateQueries({ queryKey: api.dropdownSettings.list.queryKey() });
      qc.invalidateQueries({
        queryKey: api.dropdownSettings.getById.queryKey({ id }),
      });
    },
  });
};

export const useDeleteDropdownSetting = () => {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: string) => api.dropdownSettings.remove.call({ id }),
    onSuccess: () =>
      qc.invalidateQueries({ queryKey: api.dropdownSettings.list.queryKey() }),
  });
};

export const useReplaceDropdownOptions = () => {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      settingId,
      options,
    }: {
      settingId: string;
      options: CreateDropdownOptionDto[];
    }) => api.dropdownSettings.replaceOptions.call({ id: settingId, options }),
    onSuccess: (_data, { settingId }) => {
      qc.invalidateQueries({ queryKey: api.dropdownSettings.list.queryKey() });
      qc.invalidateQueries({
        queryKey: api.dropdownSettings.getById.queryKey({ id: settingId }),
      });
    },
  });
};

export const useAddDropdownOption = () => {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      settingId,
      dto,
    }: {
      settingId: string;
      dto: CreateDropdownOptionDto;
    }) => api.dropdownSettings.addOption.call({ id: settingId, dto }),
    onSuccess: (_data, { settingId }) => {
      qc.invalidateQueries({ queryKey: api.dropdownSettings.list.queryKey() });
      qc.invalidateQueries({
        queryKey: api.dropdownSettings.getById.queryKey({ id: settingId }),
      });
    },
  });
};

export const useUpdateDropdownOption = () => {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      optionId,
      dto,
    }: {
      optionId: string;
      dto: UpdateDropdownOptionDto;
    }) => api.dropdownSettings.updateOption.call({ optionId, dto }),
    onSuccess: () =>
      qc.invalidateQueries({ queryKey: api.dropdownSettings.list.queryKey() }),
  });
};

export const useRemoveDropdownOption = () => {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (optionId: string) =>
      api.dropdownSettings.removeOption.call({ optionId }),
    onSuccess: () =>
      qc.invalidateQueries({ queryKey: api.dropdownSettings.list.queryKey() }),
  });
};
