import { useMutation, useQuery } from "@tanstack/react-query";
import * as api from "../service/IconService";
import {
  GroupIconsResponse,
  ReportsResponse,
  SystemRolesResponse,
  ShopAdminsResponse,
  UpdateShopType,
} from "../types/Types";

export const useGetIcons = () => {
  return useQuery({
    queryKey: ["dkala-icons"],
    queryFn: api.getIcons,
  });
};

export const useCreateGroupIcon = () => {
  return useMutation({
    mutationFn: api.createGroupIcon,
  });
};

export const useGetGroupIcons = () => {
  return useQuery<GroupIconsResponse>({
    queryKey: ["dkala-group-icons"],
    queryFn: api.getGroupIcons,
  });
};

export const useDeleteGroupIcon = () => {
  return useMutation({
    mutationFn: (id: string) => api.deleteGroupIcon(id),
  });
};

export const useCreateIcon = () => {
  return useMutation({
    mutationFn: api.createIcon,
  });
};

export const useDeleteIcon = () => {
  return useMutation({
    mutationFn: (id: string) => api.deleteIcon(id),
  });
};

export const useGetReports = () => {
  return useQuery<ReportsResponse>({
    queryKey: ["dkala-reports"],
    queryFn: api.getReports,
  });
};

export const useGetShops = (page: number = 1, limit: number = 10) => {
  return useQuery({
    queryKey: ["shops", page, limit],
    queryFn: () => api.getShops(page, limit),
  });
};

export const useGetShopAdmins = (shopId: string) => {
  return useQuery<ShopAdminsResponse>({
    queryKey: ["shop-admins", shopId],
    queryFn: () => api.getShopAdmins(shopId),
    enabled: !!shopId,
  });
};

export const useGetShopSmsCount = () => {
  return useQuery({
    queryKey: ["shop-sms-count"],
    queryFn: api.getShopSmsCount,
  });
};

export const useDeleteShopAdmin = () => {
  return useMutation({
    mutationFn: ({
      shopId,
      adminId,
    }: {
      shopId: string;
      adminId: string;
    }) => api.deleteShopAdmin(shopId, adminId),
  });
};

export const useAddAdminShop = () => {
  return useMutation({
    mutationFn: api.addAdminShop,
  });
};

export const useGetSystemRoles = () => {
  return useQuery<SystemRolesResponse>({
    queryKey: ["dkala-system-roles"],
    queryFn: api.getSystemRoles,
  });
};

export const useUpdateShop = () => {
  return useMutation({
    mutationFn: ({
      id,
      params,
    }: {
      id: string;
      params: UpdateShopType;
    }) => api.updateShop(id, params),
  });
};

export const useGetShop = (id: string) => {
  return useQuery({
    queryKey: ["shop", id],
    queryFn: () => api.getShop(id),
    enabled: !!id,
  });
};

export const useHardDeleteShop = () => {
  return useMutation({
    mutationFn: (id: string) => api.hardDeleteShop(id),
  });
};
