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

import { api } from "@/services/api";
import type {
  CreateFileUploadFieldDto,
  CreateFileUploadSettingDto,
  UpdateFileUploadFieldDto,
  UpdateFileUploadSettingDto,
} from "@/types/fileUploadSettings";

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

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

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

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

export const useReplaceFileUploadFields = () => {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      settingId,
      fields,
    }: {
      settingId: string;
      fields: CreateFileUploadFieldDto[];
    }) => api.fileUploadSettings.replaceFields.call({ id: settingId, fields }),
    onSuccess: (_data, { settingId }) => {
      qc.invalidateQueries({
        queryKey: api.fileUploadSettings.list.queryKey(),
      });
      qc.invalidateQueries({
        queryKey: api.fileUploadSettings.getById.queryKey({ id: settingId }),
      });
    },
  });
};

export const useAddFileUploadField = () => {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      settingId,
      dto,
    }: {
      settingId: string;
      dto: CreateFileUploadFieldDto;
    }) => api.fileUploadSettings.addField.call({ settingId, dto }),
    onSuccess: (_data, { settingId }) => {
      qc.invalidateQueries({
        queryKey: api.fileUploadSettings.list.queryKey(),
      });
      qc.invalidateQueries({
        queryKey: api.fileUploadSettings.getById.queryKey({ id: settingId }),
      });
    },
  });
};

export const useUpdateFileUploadField = () => {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      fieldId,
      dto,
    }: {
      fieldId: string;
      dto: UpdateFileUploadFieldDto;
    }) => api.fileUploadSettings.updateField.call({ fieldId, dto }),
    onSuccess: () =>
      qc.invalidateQueries({
        queryKey: api.fileUploadSettings.list.queryKey(),
      }),
  });
};

export const useRemoveFileUploadField = () => {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (fieldId: string) =>
      api.fileUploadSettings.removeField.call({ fieldId }),
    onSuccess: () =>
      qc.invalidateQueries({
        queryKey: api.fileUploadSettings.list.queryKey(),
      }),
  });
};
