import client from "../client"
import type { ApiResultBorrowInfoVo, ApiResultListBorrowInfoVo, ApiResultString, BorrowInfoVo } from "@/types/api"

export async function getAllBorrows(): Promise<BorrowInfoVo[]> {
  const res = await client.get<ApiResultListBorrowInfoVo>("/api/admin/borrows/getall")
  return res.data.data!
}

export async function getOneBorrow(id: number): Promise<BorrowInfoVo> {
  const res = await client.get<ApiResultBorrowInfoVo>("/api/admin/borrows/getone", { params: { id } })
  return res.data.data!
}

export async function searchBorrows(query: string): Promise<BorrowInfoVo[]> {
  const res = await client.get<ApiResultListBorrowInfoVo>("/api/admin/borrows/search", { params: { query } })
  return res.data.data!
}

export async function borrowBook(bookId: number, userId: number): Promise<string | null> {
  const res = await client.post<ApiResultString>("/api/admin/borrows/borrowbook", null, { params: { bookId, userId } })
  return res.data.data
}

export async function returnBook(recordId: number): Promise<string | null> {
  const res = await client.post<ApiResultString>("/api/admin/borrows/returnbook", null, { params: { recordId } })
  return res.data.data
}
