'use client'
import { useMutation } from '@tanstack/react-query'
import { BuildAddressUpdate } from './types';

const fetchGraphQlBuildAddressUpdate = async (record: BuildAddressUpdate, uuid: string): Promise<{ data: BuildAddressUpdate | null; status: number }> => {
    console.log('Fetching test data from local API');
    try {
        const res = await fetch(`/api/build-address/update?uuid=${uuid || ''}`, { method: 'POST', cache: 'no-store', credentials: "include", body: JSON.stringify(record) });
        if (!res.ok) { const errorText = await res.text(); console.error('Test data API error:', errorText); throw new Error(`API error: ${res.status} ${res.statusText}`) }
        const data = await res.json();
        return { data: data.data, status: res.status }
    } catch (error) { console.error('Error fetching test data:', error); throw error }
};

export function useUpdateBuildAddressMutation() {
    return useMutation({
        mutationFn: ({ data, uuid }: { data: BuildAddressUpdate, uuid: string }) => fetchGraphQlBuildAddressUpdate(data, uuid),
        onSuccess: () => { console.log("Build Address updated successfully") },
        onError: (error) => { console.error("Update Build Address failed:", error) },
    })
}
