import type { Request, Response } from "express";
import { BoxReturnService } from "../services/BoxReturnService.js";

const boxReturnService = new BoxReturnService();

export const getSupplierBoxLedger = async (req: Request, res: Response): Promise<void> => {
  try {
    const search = req.query.search as string | undefined;
    const ledger = await boxReturnService.getSupplierBoxLedger(search);

    res.status(200).json({
      success: true,
      message: "Supplier box ledger fetched successfully",
      result: ledger,
    });
  } catch (error: any) {
    const message = error instanceof Error ? error.message : "Failed to fetch box ledger";
    res.status(400).json({ message });
  }
};

export const getIncomingBoxHistory = async (req: Request, res: Response): Promise<void> => {
  try {
    const { supplierId, startDate, endDate, page, limit, search } = req.query;

    const filters = {
      supplierId: supplierId as string,
      startDate: startDate as string,
      endDate: endDate as string,
      search: search as string,
    };

    const parsedPage = page ? parseInt(page as string, 10) : 1;
    const parsedLimit = limit ? parseInt(limit as string, 10) : 20;

    const result = await boxReturnService.getIncomingBoxHistory(filters, parsedPage, parsedLimit);

    res.status(200).json({
      success: true,
      message: "Incoming box history fetched successfully",
      result,
    });
  } catch (error: any) {
    const message = error instanceof Error ? error.message : "Failed to fetch incoming box history";
    res.status(400).json({ message });
  }
};

export const getBoxReturns = async (req: Request, res: Response): Promise<void> => {
  try {
    const { supplierId, startDate, endDate, page, limit, search } = req.query;

    const filters = {
      supplierId: supplierId as string,
      startDate: startDate as string,
      endDate: endDate as string,
      search: search as string,
    };

    const parsedPage = page ? parseInt(page as string, 10) : 1;
    const parsedLimit = limit ? parseInt(limit as string, 10) : 20;

    const result = await boxReturnService.getBoxReturns(filters, parsedPage, parsedLimit);

    res.status(200).json({
      success: true,
      message: "Box returns fetched successfully",
      result,
    });
  } catch (error: any) {
    const message = error instanceof Error ? error.message : "Failed to fetch box returns";
    res.status(400).json({ message });
  }
};

export const getBoxReturnById = async (req: Request, res: Response): Promise<void> => {
  try {
    const id = BigInt(req.params.id as string);
    const record = await boxReturnService.getBoxReturnById(id);

    res.status(200).json({
      success: true,
      message: "Box return fetched successfully",
      result: record,
    });
  } catch (error: any) {
    const message = error instanceof Error ? error.message : "Failed to fetch box return";
    res.status(400).json({ message });
  }
};

export const createBoxReturn = async (req: Request, res: Response): Promise<void> => {
  try {
    const data = {
      ...req.body,
      createdBy: req.user?.id ? BigInt(req.user.id) : undefined,
    };
    const record = await boxReturnService.createBoxReturn(data);

    res.status(201).json({
      success: true,
      message: "Box return recorded successfully",
      result: record,
    });
  } catch (error: any) {
    const message = error instanceof Error ? error.message : "Failed to record box return";
    res.status(400).json({ message });
  }
};

export const updateBoxReturn = async (req: Request, res: Response): Promise<void> => {
  try {
    const id = BigInt(req.params.id as string);
    const record = await boxReturnService.updateBoxReturn(id, req.body);

    res.status(200).json({
      success: true,
      message: "Box return updated successfully",
      result: record,
    });
  } catch (error: any) {
    const message = error instanceof Error ? error.message : "Failed to update box return";
    res.status(400).json({ message });
  }
};

export const deleteBoxReturn = async (req: Request, res: Response): Promise<void> => {
  try {
    const id = BigInt(req.params.id as string);
    await boxReturnService.deleteBoxReturn(id);

    res.status(200).json({
      success: true,
      message: "Box return deleted successfully",
    });
  } catch (error: any) {
    const message = error instanceof Error ? error.message : "Failed to delete box return";
    res.status(400).json({ message });
  }
};
