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

/**
 * Hook for fetching all coupons (admin only)
 */
export const useAdminCoupons = () => {
  return useQuery({
    queryKey: ['admin-coupons'],
    queryFn: couponService.getAllCoupons
  });
};

/**
 * Hook for fetching a single coupon by ID (admin only)
 * @param {string} id - Coupon ID
 */
export const useAdminCoupon = (id) => {
  return useQuery({
    queryKey: ['admin-coupon', id],
    queryFn: () => couponService.getCouponById(id),
    enabled: !!id
  });
};

/**
 * Hook for creating a new coupon (admin only)
 */
export const useCreateCoupon = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();
  
  return useMutation({
    mutationFn: (couponData) => couponService.createCoupon(couponData),
    onSuccess: (data) => {
      queryClient.invalidateQueries({ queryKey: ['admin-coupons'] });
      notification.showNotification('Coupon created successfully', 'success');
      return data;
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to create coupon',
        'error'
      );
      throw error;
    },
  });
};

/**
 * Hook for updating a coupon (admin only)
 */
export const useUpdateCoupon = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();
  
  return useMutation({
    mutationFn: ({ id, couponData }) => couponService.updateCoupon(id, couponData),
    onSuccess: (data) => {
      queryClient.invalidateQueries({ queryKey: ['admin-coupons'] });
      queryClient.invalidateQueries({ queryKey: ['admin-coupon', data.coupon?.id] });
      notification.showNotification('Coupon updated successfully', 'success');
      return data;
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to update coupon',
        'error'
      );
      throw error;
    },
  });
};

/**
 * Hook for deleting a coupon (admin only)
 */
export const useDeleteCoupon = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();
  
  return useMutation({
    mutationFn: (id) => couponService.deleteCoupon(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['admin-coupons'] });
      notification.showNotification('Coupon deleted successfully', 'success');
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to delete coupon',
        'error'
      );
      throw error;
    },
  });
};

/**
 * Hook for fetching coupon redemption history (admin only)
 */
export const useCouponRedemptions = (id) => {
  return useQuery({
    queryKey: ['admin-coupon-redemptions', id],
    queryFn: () => couponService.getCouponRedemptions(id),
    enabled: !!id
  });
};

export default {
  useAdminCoupons,
  useAdminCoupon,
  useCreateCoupon,
  useUpdateCoupon,
  useDeleteCoupon,
  useCouponRedemptions
};