const OffboardingService = require('../services/offboarding.service');
const { asyncHandler } = require('../middleware/errorHandler');

class OffboardingController {
    static getAllProtocols = asyncHandler(async (req, res) => {
        const protocols = OffboardingService.getAllProtocols();
        res.json({ status: 'success', data: protocols });
    });

    static getProtocolById = asyncHandler(async (req, res) => {
        const protocol = OffboardingService.getProtocolById(parseInt(req.params.id));
        res.json({ status: 'success', data: protocol });
    });

    static createProtocol = asyncHandler(async (req, res) => {
        const protocol = await OffboardingService.createProtocol(req.body, req.user.id, req.ip);
        res.status(201).json({ status: 'success', data: protocol });
    });

    static updateProtocol = asyncHandler(async (req, res) => {
        const protocol = await OffboardingService.updateProtocol(
            parseInt(req.params.id),
            req.body,
            req.user.id,
            req.ip
        );
        res.json({ status: 'success', data: protocol });
    });

    static returnAssets = asyncHandler(async (req, res) => {
        const protocol = await OffboardingService.returnAssets(
            parseInt(req.params.id),
            req.body.asset_returns,
            req.user.id,
            req.ip
        );
        res.json({ status: 'success', data: protocol });
    });

    static regeneratePdf = asyncHandler(async (req, res) => {
        const protocol = await OffboardingService.regeneratePdf(
            parseInt(req.params.id),
            req.user.id,
            req.ip
        );
        res.json({ status: 'success', data: protocol });
    });

    static deleteProtocol = asyncHandler(async (req, res) => {
        const deleted = OffboardingService.deleteProtocol(
            parseInt(req.params.id),
            req.user.id,
            req.ip
        );

        if (!deleted) {
            return res.status(404).json({ status: 'error', message: 'Protokoll nicht gefunden' });
        }

        res.json({ status: 'success', message: 'Protokoll erfolgreich gelöscht' });
    });

    static getStatistics = asyncHandler(async (req, res) => {
        const stats = OffboardingService.getStatistics();
        res.json({ status: 'success', data: stats });
    });
}

module.exports = OffboardingController;
