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

/**
 * The customer's **own** review for this booking + its current moderation state (`none` when not yet
 * reviewed). Drives the persistent "under review" / "published" state on the leave-a-review CTA so a returning
 * customer never sees a second form. Keyed per booking; **invalidated on submit**.
 */
export function useMyReviewForBooking(bookingId: number, options?: { enabled?: boolean }) {
  return useQuery({
    queryKey: reviewKeys.myReviewForBooking(bookingId),
    queryFn: () => reviewsApi.getMyReviewForBooking(bookingId),
    enabled: (options?.enabled ?? true) && bookingId > 0,
    staleTime: REVIEW_ELIGIBILITY_STALE_TIME,
    gcTime: REVIEWS_GC_TIME,
  });
}
