import { toast } from 'react-toastify';
import type { Booking } from '../../features/bookings/services/bookingService';

/**
 * Validate that a booking belongs to the current user
 * @param booking - The booking to validate
 * @param currentUserId - The current user's ID
 * @returns true if booking belongs to user, false otherwise
 */
export const validateBookingOwnership = (
  booking: Booking | null,
  currentUserId: number | undefined
): boolean => {
  if (!booking || !currentUserId) {
    return false;
  }

  if (booking.user_id !== currentUserId) {
    return false;
  }

  return true;
};

/**
 * Validate booking ownership and show error if invalid
 * @param booking - The booking to validate
 * @param currentUserId - The current user's ID
 * @param navigate - Navigation function
 * @param redirectPath - Path to redirect to if invalid
 * @returns true if valid, false if invalid (and redirects)
 */
export const validateAndHandleBookingOwnership = (
  booking: Booking | null,
  currentUserId: number | undefined,
  navigate: (path: string) => void,
  redirectPath: string = '/bookings'
): boolean => {
  if (!validateBookingOwnership(booking, currentUserId)) {
    toast.error('You do not have permission to view this booking');
    navigate(redirectPath);
    return false;
  }
  return true;
};

