"use client";

import { useMutation, useQueryClient } from "@tanstack/react-query";
import { apiClient, queryKeys } from "@/core/api";
import type { CreateCaseRequest, CreateCaseResponse } from "@customer-portal/domain/support";

export function useCreateCase() {
  const queryClient = useQueryClient();

  return useMutation<CreateCaseResponse, Error, CreateCaseRequest>({
    mutationFn: async (data: CreateCaseRequest) => {
      const response = await apiClient.POST<CreateCaseResponse>("/api/support/cases", {
        body: data,
      });

      if (!response.data) {
        throw new Error("Failed to create support case");
      }

      return response.data;
    },
    onSuccess: () => {
      // Invalidate cases list to refetch
      void queryClient.invalidateQueries({ queryKey: queryKeys.support.cases() });
    },
  });
}
