"use client";

import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { queryKeys } from "@/core/api";
import { servicesService } from "@/features/services/api";
import type { Address } from "@customer-portal/domain/customer";

export function useInternetEligibility(options?: { enabled?: boolean | undefined }) {
  return useQuery({
    queryKey: queryKeys.services.internet.eligibility(),
    queryFn: () => servicesService.getInternetEligibility(),
    enabled: options?.enabled ?? true,
  });
}

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

  return useMutation({
    mutationFn: (body?: { notes?: string; address?: Partial<Address> }) =>
      servicesService.requestInternetEligibilityCheck(body),
    onSuccess: async () => {
      await queryClient.invalidateQueries({ queryKey: queryKeys.services.internet.eligibility() });
      await queryClient.invalidateQueries({ queryKey: queryKeys.services.internet.combined() });
    },
  });
}
