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

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

const KEY = ["file-upload-settings"] as const;

/* ------------------------------ Queries ------------------------------ */

export const useFileUploadSettings = () =>
  useQuery({
    queryKey: KEY,
    queryFn: fileUploadSettingsService.list,
  });

export const useFileUploadSetting = (id: string) =>
  useQuery({
    queryKey: [...KEY, "id", id],
    queryFn: () => fileUploadSettingsService.getById(id),
    enabled: Boolean(id),
  });

export const useFileUploadSettingByCode = (code: string) =>
  useQuery({
    queryKey: [...KEY, "code", code],
    queryFn: () => fileUploadSettingsService.getByCode(code),
    enabled: Boolean(code),
  });

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

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

export const useUpdateFileUploadSetting = () => {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      id,
      dto,
    }: {
      id: string;
      dto: UpdateFileUploadSettingDto;
    }) => fileUploadSettingsService.update(id, dto),
    onSuccess: (_data, { id }) => {
      qc.invalidateQueries({ queryKey: KEY });
      qc.invalidateQueries({ queryKey: [...KEY, "id", id] });
    },
  });
};

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

export const useReplaceFileUploadFields = () => {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      settingId,
      fields,
    }: {
      settingId: string;
      fields: CreateFileUploadFieldDto[];
    }) => fileUploadSettingsService.replaceFields(settingId, fields),
    onSuccess: (_data, { settingId }) => {
      qc.invalidateQueries({ queryKey: KEY });
      qc.invalidateQueries({ queryKey: [...KEY, "id", settingId] });
    },
  });
};

export const useAddFileUploadField = () => {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      settingId,
      dto,
    }: {
      settingId: string;
      dto: CreateFileUploadFieldDto;
    }) => fileUploadSettingsService.addField(settingId, dto),
    onSuccess: (_data, { settingId }) => {
      qc.invalidateQueries({ queryKey: KEY });
      qc.invalidateQueries({ queryKey: [...KEY, "id", settingId] });
    },
  });
};

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

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