import * as api from "../service/TicketService";
import type {
  CreateTicketCategoryType,
  CreateTicketMessageType,
} from "../types/Types";
import { useMutation, useQuery } from "@tanstack/react-query";

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

export const useGetTicketCategories = () => {
  return useQuery({
    queryKey: ["ticket-categories"],
    queryFn: api.getTicketCategories,
  });
};

export const useGetTicketCategory = (categoryId: string) => {
  return useQuery({
    queryKey: ["ticket-category", categoryId],
    queryFn: () => api.getTicketCategory(categoryId),
    enabled: !!categoryId,
  });
};

export const useAddTicketMessage = () => {
  return useMutation({
    mutationFn: ({
      ticketId,
      params,
    }: {
      ticketId: string;
      params: CreateTicketMessageType;
    }) => api.addTicketMessage(ticketId, params),
  });
};

export const useGetTicket = (ticketId: string) => {
  return useQuery({
    queryKey: ["ticket", ticketId],
    queryFn: () => api.getTicket(ticketId),
  });
};

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

export const useUpdateTicketCategory = () => {
  return useMutation({
    mutationFn: ({
      categoryId,
      params,
    }: {
      categoryId: string;
      params: CreateTicketCategoryType;
    }) => api.updateTicketCategory(categoryId, params),
  });
};

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