import { useQuery } from '@tanstack/react-query';
import { reviewsApi } from '../apis';
import { reviewKeys } from '../keys';
import { REVIEW_ELIGIBILITY_STALE_TIME, REVIEWS_GC_TIME } from '../constants';

/**
 * Whether *this* booking can still be reviewed (completed/closed AND not already reviewed). Keyed per booking;
 * short `staleTime` and **invalidated on submit** so the leave-a-review CTA flips to the "under review" state
 * immediately. `enabled` lets the caller defer until it holds a real booking id.
 */
export function useReviewEligibility(bookingId: number, options?: { enabled?: boolean }) {
  return useQuery({
    queryKey: reviewKeys.eligibility(bookingId),
    queryFn: () => reviewsApi.getReviewEligibility(bookingId),
    enabled: (options?.enabled ?? true) && bookingId > 0,
    staleTime: REVIEW_ELIGIBILITY_STALE_TIME,
    gcTime: REVIEWS_GC_TIME,
  });
}
