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

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

const KEY = ["dropdown-settings"] as const;

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

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

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

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

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

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

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

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

export const useReplaceDropdownOptions = () => {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      settingId,
      options,
    }: {
      settingId: string;
      options: CreateDropdownOptionDto[];
    }) => dropdownSettingsService.replaceOptions(settingId, options),
    onSuccess: (_data, { settingId }) => {
      qc.invalidateQueries({ queryKey: KEY });
      qc.invalidateQueries({ queryKey: [...KEY, "id", settingId] });
    },
  });
};

export const useAddDropdownOption = () => {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      settingId,
      dto,
    }: {
      settingId: string;
      dto: CreateDropdownOptionDto;
    }) => dropdownSettingsService.addOption(settingId, dto),
    onSuccess: (_data, { settingId }) => {
      qc.invalidateQueries({ queryKey: KEY });
      qc.invalidateQueries({ queryKey: [...KEY, "id", settingId] });
    },
  });
};

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

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