import axios from "../../../config/axios";
import {
  AddMessageTicketType,
  CreateCategoryType,
  UpdateCategoryType,
} from "../types/TicketTypes";

export const getTickets = async (status: string, customerId?: string) => {
  const params = new URLSearchParams();
  if (status) params.append("status", status);
  if (customerId) params.append("userId", customerId);
  const query = params.toString();
  const { data } = await axios.get(`/tickets/admin?${query}`);
  return data;
};

export const getMessages = async (id: string) => {
  const { data } = await axios.get(`/tickets/${id}/messages`);
  return data;
};

export const addMessageTicket = async (
  id: string,
  params: AddMessageTicketType
) => {
  const { data } = await axios.post(`/tickets/${id}/messages`, params);
  return data;
};

export const closeTicket = async (id: string) => {
  const { data } = await axios.post(`/tickets/${id}/close`);
  return data;
};

export const createCategory = async (params: CreateCategoryType) => {
  const { data } = await axios.post(`/tickets/category`, params);
  return data;
};

export const getCategories = async () => {
  const { data } = await axios.get(`/tickets/categories`);
  return data;
};

export const referTicket = async (id: string, userId: string) => {
  const { data } = await axios.post(`/tickets/${id}/refer`, {
    adminId: userId,
  });
  return data;
};

export const openTicket = async (id: string) => {
  const { data } = await axios.post(`/tickets/${id}/open`);
  return data;
};

export const updateCategory = async (
  id: string,
  params: UpdateCategoryType
) => {
  const { data } = await axios.patch(`/tickets/category/${id}`, params);
  return data;
};

export const deleteCategory = async (id: string) => {
  const { data } = await axios.delete(`/tickets/category/${id}`);
  return data;
};
