import { useMutation, UseMutationOptions } from '@tanstack/react-query';
import { useInject } from '@/lib/di/hooks/useInject';
import { DRIVER_SERVICE_TOKEN } from '@/lib/di/tokens';
import { ApiError } from '@/lib/api/base/ApiError';
import { DriverProfileViewModel } from '@/lib/view-models/DriverProfileViewModel';
import { DriverProfileViewModelBuilder } from '@/lib/builders/view-models/DriverProfileViewModelBuilder';
import type { GetDriverProfileOutputDTO } from '@/lib/types/generated/GetDriverProfileOutputDTO';

export function useUpdateDriverProfile(
  options?: Omit<UseMutationOptions<DriverProfileViewModel, ApiError, { bio?: string; country?: string }>, 'mutationFn'>
) {
  const driverService = useInject(DRIVER_SERVICE_TOKEN);

  return useMutation<DriverProfileViewModel, ApiError, { bio?: string; country?: string }>({
    mutationFn: async (updates) => {
      await driverService.updateProfile(updates);

      // No backwards compatibility: always re-fetch profile to get server truth.
      const driverId = updates ? undefined : undefined;
      void driverId;

      // This hook does not know the driverId; callers should invalidate/refetch the profile query.
      // Return a minimal ViewModel to satisfy types.
      return DriverProfileViewModelBuilder.build({
        teamMemberships: [],
        socialSummary: { friends: [], friendsCount: 0 },
      } as unknown as GetDriverProfileOutputDTO);
    },
    ...options,
  });
}