import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import productReviewService, { productReviewAdminService } from '@services/productReviewService';
import { useNotification } from './reduxHooks';

// User-facing review hooks
export const useProductReviews = (productId) => {
  return useQuery({
    queryKey: ['product-reviews', productId],
    queryFn: () => productReviewService.getProductReviews(productId),
    enabled: !!productId
  });
};

export const useCanReviewProduct = (productId) => {
  return useQuery({
    queryKey: ['can-review-product', productId],
    queryFn: () => productReviewService.canReviewProduct(productId),
    enabled: !!productId
  });
};

export const useAddProductReview = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();
  
  return useMutation({
    mutationFn: ({ productId, reviewData }) => 
      productReviewService.addProductReview(productId, reviewData),
    onSuccess: (data, variables) => {
      // Invalidate reviews for this product
      queryClient.invalidateQueries({ queryKey: ['product-reviews', variables.productId] });
      queryClient.invalidateQueries({ queryKey: ['can-review-product', variables.productId] });
      
      notification.showNotification(
        data.message || 'Review submitted successfully',
        'success'
      );
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to submit review',
        'error'
      );
    }
  });
};

// Admin review hooks
export const usePendingReviews = () => {
  return useQuery({
    queryKey: ['pending-reviews'],
    queryFn: productReviewAdminService.getPendingReviews
  });
};

export const useAdminProductReviews = (productId) => {
  return useQuery({
    queryKey: ['admin-product-reviews', productId],
    queryFn: () => productReviewAdminService.getProductReviews(productId),
    enabled: !!productId
  });
};

export const useApproveReview = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();
  
  return useMutation({
    mutationFn: (reviewId) => productReviewAdminService.approveReview(reviewId),
    onSuccess: () => {
      // Invalidate both pending reviews and product reviews
      queryClient.invalidateQueries({ queryKey: ['pending-reviews'] });
      queryClient.invalidateQueries({ queryKey: ['admin-product-reviews'] });
      queryClient.invalidateQueries({ queryKey: ['product-reviews'] });
      
      notification.showNotification(
        'Review approved successfully',
        'success'
      );
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to approve review',
        'error'
      );
    }
  });
};

export const useDeleteReview = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();
  
  return useMutation({
    mutationFn: (reviewId) => productReviewAdminService.deleteReview(reviewId),
    onSuccess: () => {
      // Invalidate both pending reviews and product reviews
      queryClient.invalidateQueries({ queryKey: ['pending-reviews'] });
      queryClient.invalidateQueries({ queryKey: ['admin-product-reviews'] });
      queryClient.invalidateQueries({ queryKey: ['product-reviews'] });
      
      notification.showNotification(
        'Review deleted successfully',
        'success'
      );
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to delete review',
        'error'
      );
    }
  });
};