import libraryService from "../services/libraryService.js";
import { ApiError, asyncHandler } from "../utils/helpers.js";

export const addBorrow = asyncHandler(async (req, res) => {
    const { bookId, clientId, dueDate } = req.body;
    if (!bookId || !clientId || !dueDate) {
        throw new ApiError(400, "Missing required fields");
    }

    await libraryService.addBorrow(bookId, clientId, dueDate);
    res.status(201).json({ message: "Borrow added successfully" });
});

export const getAllBorrows = asyncHandler(async (req, res) => {
    const borrows = await libraryService.getAllBorrows();
    res.status(200).json(borrows);
});

export const updateBorrow = asyncHandler(async (req, res) => {
    const { bookId, clientId, dueDate } = req.body;
    if (!bookId || !clientId || !dueDate) {
        throw new ApiError(400, "Missing required fields");
    }

    await libraryService.updateBorrow(req.params.id, bookId, clientId, dueDate);
    res.status(200).json({ message: "Borrow updated successfully" });
});

export const deleteBorrow = asyncHandler(async (req, res) => {
    await libraryService.deleteBorrow(req.params.id);
    res.status(200).json({ message: "Borrow deleted successfully" });
});