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

// Public blog hooks
export const useBlogPosts = (params) => {
  return useQuery({
    queryKey: ['blog-posts', params],
    queryFn: () => blogService.getAllPosts(params),
  });
};

export const useBlogPost = (slug) => {
  return useQuery({
    queryKey: ['blog-post', slug],
    queryFn: () => blogService.getPostBySlug(slug),
    enabled: !!slug,
  });
};

export const useBlogCategories = () => {
  return useQuery({
    queryKey: ['blog-categories'],
    queryFn: () => blogService.getAllCategories(),
  });
};

export const useAddComment = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();
  
  return useMutation({
    mutationFn: ({ postId, commentData }) => blogService.addComment(postId, commentData),
    onSuccess: (data, variables) => {
      queryClient.invalidateQueries({ queryKey: ['blog-post', variables.postId] });
      notification.showNotification(
        data.message || 'Comment added successfully', 
        'success'
      );
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to add comment', 
        'error'
      );
    },
  });
};

// Admin blog hooks
export const useAdminBlogPosts = () => {
  return useQuery({
    queryKey: ['admin-blog-posts'],
    queryFn: () => blogAdminService.getAllPosts(),
  });
};

export const useAdminBlogPost = (id) => {
  return useQuery({
    queryKey: ['admin-blog-post', id],
    queryFn: () => blogAdminService.getPostById(id),
    enabled: !!id,
  });
};

export const useCreateBlogPost = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();
  
  return useMutation({
    mutationFn: (postData) => blogAdminService.createPost(postData),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['admin-blog-posts'] });
      notification.showNotification(
        'Blog post created successfully', 
        'success'
      );
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to create blog post', 
        'error'
      );
    },
  });
};

export const useUpdateBlogPost = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();
  
  return useMutation({
    mutationFn: ({ id, postData }) => blogAdminService.updatePost(id, postData),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({ queryKey: ['admin-blog-posts'] });
      queryClient.invalidateQueries({ queryKey: ['admin-blog-post', variables.id] });
      notification.showNotification(
        'Blog post updated successfully', 
        'success'
      );
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to update blog post', 
        'error'
      );
    },
  });
};

export const useDeleteBlogPost = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();
  
  return useMutation({
    mutationFn: (id) => blogAdminService.deletePost(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['admin-blog-posts'] });
      notification.showNotification(
        'Blog post deleted successfully', 
        'success'
      );
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to delete blog post', 
        'error'
      );
    },
  });
};

export const useUploadBlogImage = () => {
  const notification = useNotification();
  
  return useMutation({
    mutationFn: ({ postId, imageData }) => blogAdminService.uploadImage(postId, imageData),
    onSuccess: (_, variables) => {
      notification.showNotification(
        'Image uploaded successfully', 
        'success'
      );
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to upload image', 
        'error'
      );
    },
  });
};

export const useDeleteBlogImage = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();
  
  return useMutation({
    mutationFn: ({ postId, imageId }) => blogAdminService.deleteImage(postId, imageId),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({ queryKey: ['admin-blog-post', variables.postId] });
      notification.showNotification(
        'Image deleted successfully', 
        'success'
      );
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to delete image', 
        'error'
      );
    },
  });
};

export const usePendingComments = () => {
  return useQuery({
    queryKey: ['pending-comments'],
    queryFn: () => blogAdminService.getPendingComments(),
  });
};

export const usePostComments = (postId) => {
  return useQuery({
    queryKey: ['post-comments', postId],
    queryFn: () => blogAdminService.getPostComments(postId),
    enabled: !!postId,
  });
};

export const useApproveComment = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();
  
  return useMutation({
    mutationFn: (commentId) => blogAdminService.approveComment(commentId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['pending-comments'] });
      queryClient.invalidateQueries({ queryKey: ['post-comments'] });
      notification.showNotification(
        'Comment approved successfully', 
        'success'
      );
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to approve comment', 
        'error'
      );
    },
  });
};

export const useDeleteComment = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();
  
  return useMutation({
    mutationFn: (commentId) => blogAdminService.deleteComment(commentId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['pending-comments'] });
      queryClient.invalidateQueries({ queryKey: ['post-comments'] });
      notification.showNotification(
        'Comment deleted successfully', 
        'success'
      );
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to delete comment', 
        'error'
      );
    },
  });
};