import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"
import { toast } from "sonner"
import { getAllBooks, searchBook } from "@/api/books"
import { addBook, deleteBook, updateBook, updateStock } from "@/api/admin/books"
import { getAllBorrows, searchBorrows, returnBook, borrowBook as adminBorrowBook } from "@/api/admin/borrows"
import { getErrorMessage } from "@/lib/errors"

export function useAdminBooks() {
  return useQuery({
    queryKey: ["admin", "books"],
    queryFn: getAllBooks,
  })
}

export function useAdminSearchBooks(query: string) {
  return useQuery({
    queryKey: ["admin", "books", "search", query],
    queryFn: () => searchBook(query),
    enabled: query.length > 0,
    retry: false,
  })
}

export function useAddBook() {
  const qc = useQueryClient()
  return useMutation({
    mutationFn: addBook,
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ["admin", "books"] })
      toast.success("添加成功")
    },
    onError: (err) => {
      toast.error(getErrorMessage(err, "添加失败"))
    },
  })
}

export function useUpdateBook() {
  const qc = useQueryClient()
  return useMutation({
    mutationFn: ({ id, data }: { id: number; data: { name: string; author: string } }) =>
      updateBook(id, data),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ["admin", "books"] })
      toast.success("修改成功")
    },
    onError: (err) => {
      toast.error(getErrorMessage(err, "修改失败"))
    },
  })
}

export function useUpdateStock() {
  const qc = useQueryClient()
  return useMutation({
    mutationFn: ({ id, stock }: { id: number; stock: number }) => updateStock(id, stock),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ["admin", "books"] })
      toast.success("库存更新成功")
    },
    onError: (err) => {
      toast.error(getErrorMessage(err, "库存更新失败"))
    },
  })
}

export function useDeleteBook() {
  const qc = useQueryClient()
  return useMutation({
    mutationFn: deleteBook,
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ["admin", "books"] })
      toast.success("删除成功")
    },
    onError: (err) => {
      toast.error(getErrorMessage(err, "删除失败"))
    },
  })
}

export function useAdminBorrows() {
  return useQuery({
    queryKey: ["admin", "borrows"],
    queryFn: getAllBorrows,
  })
}

export function useSearchBorrows(query: string) {
  return useQuery({
    queryKey: ["admin", "borrows", "search", query],
    queryFn: () => searchBorrows(query),
    enabled: query.length > 0,
    retry: false,
  })
}

export function useAdminReturnBook() {
  const qc = useQueryClient()
  return useMutation({
    mutationFn: returnBook,
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ["admin", "borrows"] })
      toast.success("归还成功")
    },
    onError: (err) => {
      toast.error(getErrorMessage(err, "归还失败"))
    },
  })
}

export function useAdminBorrowBook() {
  const qc = useQueryClient()
  return useMutation({
    mutationFn: ({ bookId, userId }: { bookId: number; userId: number }) =>
      adminBorrowBook(bookId, userId),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ["admin", "borrows"] })
      toast.success("借阅成功")
    },
    onError: (err) => {
      toast.error(getErrorMessage(err, "借阅失败"))
    },
  })
}
