"use client";

import { useMutation } from "@tanstack/react-query";
import { apiFetch } from "@/lib/api";
import type {
  BestLayoutsRequest,
  BestLayoutsResponse,
} from "@/types/best-layouts";

/**
 * TanStack Query mutation for POST /api/v1/parcels/{cad_num}/best-layouts.
 *
 * Usage:
 *   const { mutate, data, isPending, error } = useBestLayouts(cadNum);
 *   mutate(requestBody);
 */
export function useBestLayouts(cadNum: string) {
  return useMutation({
    mutationKey: ["best-layouts", cadNum],
    mutationFn: (body: BestLayoutsRequest): Promise<BestLayoutsResponse> =>
      apiFetch<BestLayoutsResponse>(
        `/api/v1/parcels/${encodeURIComponent(cadNum)}/best-layouts`,
        {
          method: "POST",
          body: JSON.stringify(body),
        },
      ),
  });
}
