import { apiEndpoints } from "@/lib/api/endpoints";
import { fetchWithAuth } from "@/lib/auth/client";
import type {
  SupportTicketDetailResponse,
  SupportTicketStatus,
  SupportTicketsResponse,
  SupportTicketUpdateResponse,
  SupportMessageResponse,
} from "@/types/api";

export async function listSupportTickets(
  params: Record<string, string | number | boolean | null | undefined> = {},
) {
  return fetchWithAuth<SupportTicketsResponse>(apiEndpoints.support.adminTickets(params));
}

export async function getSupportTicket(ticketId: string) {
  return fetchWithAuth<SupportTicketDetailResponse>(apiEndpoints.support.adminTicket(ticketId));
}

export async function replyToSupportTicket(ticketId: string, message: string, image?: File | null) {
  const formData = new FormData();
  formData.set("message", message);
  if (image) {
    formData.set("image", image);
  }
  return fetchWithAuth<SupportMessageResponse>(apiEndpoints.support.adminReply(ticketId), {
    method: "POST",
    body: formData,
  });
}

export async function updateSupportTicketStatus(ticketId: string, status: SupportTicketStatus) {
  return fetchWithAuth<SupportTicketUpdateResponse>(apiEndpoints.support.adminStatus(ticketId), {
    method: "PATCH",
    body: JSON.stringify({ status }),
  });
}

export async function assignSupportTicket(ticketId: string, adminId: string) {
  return fetchWithAuth<SupportTicketUpdateResponse>(apiEndpoints.support.adminAssign(ticketId), {
    method: "PATCH",
    body: JSON.stringify({ adminId: adminId || null }),
  });
}
