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

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

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

export const useGetGroupIcons = () => {
  return useQuery<GroupIconsResponse>({
    queryKey: ["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: ["reports"],
    queryFn: api.getReports,
  });
};

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

export const useGetRestaurantAdmins = (restaurantId: string) => {
  return useQuery<RestaurantAdminsResponse>({
    queryKey: ["restaurant-admins", restaurantId],
    queryFn: () => api.getRestaurantAdmins(restaurantId),
    enabled: !!restaurantId,
  });
};

export const useGetRestaurantSmsCount = () => {
  return useQuery({
    queryKey: ["restaurant-sms-count"],
    queryFn: api.getRestaurantSmsCount,
  });
};

export const useDeleteRestaurantAdmin = () => {
  return useMutation({
    mutationFn: ({
      restaurantId,
      adminId,
    }: {
      restaurantId: string;
      adminId: string;
    }) => api.deleteRestaurantAdmin(restaurantId, adminId),
  });
};

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

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

export const useUpdateRestaurant = () => {
  return useMutation({
    mutationFn: ({
      id,
      params,
    }: {
      id: string;
      params: UpdateRestaurantType;
    }) => api.updateRestaurant(id, params),
  });
};

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

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