import { Request, Response } from "express";

import { ApprovalService } from "./approval.service";

const service = new ApprovalService();

export class ApprovalController {

    /**
     * Obtener todas las aprobaciones
     */
    async getAll(
        req: Request,
        res: Response
    ) {

        try {

            const approvals =
                await service.getAll();

            res.json(
                approvals
            );

        }
        catch (error) {

            res.status(500).json({
                message:
                    "Error al obtener aprobaciones",
                error
            });

        }

    }

    /**
     * Obtener aprobación por ID
     */
    async getById(
        req: Request,
        res: Response
    ) {

        try {

            const approval =
                await service.getById(
                    req.params.id as string
                );

            if (!approval) {

                return res.status(404).json({
                    message:
                        "Aprobación no encontrada"
                });

            }

            res.json(
                approval
            );

        }
        catch (error:any) {

        console.error(error);

        res.status(500).json({
        message: "Error al aprobar solicitud",
        error: error.message,
        stack: error.stack
    });

        }

    }

    /**
     * Crear aprobación
     */
    async create(
        req: Request,
        res: Response
    ) {

        try {

            const approval =
                await service.create(
                    req.body
                );

            res.status(201).json(
                approval
            );

        }
        catch (error) {

            res.status(500).json({
                message:
                    "Error al crear aprobación",
                error
            });

        }

    }

    /**
     * Aprobar solicitud
     */
    async approve(
        req: Request,
        res: Response
    ) {

        try {

            const approval =
                await service.approve(

                    req.params.id as string,

                    req.body.approvedBy,

                    req.body.comments

                );

            res.json(
                approval
            );

        }
        catch (error) {

            res.status(500).json({
                message:
                    "Error al aprobar solicitud",
                error
            });

        }

    }

    /**
     * Rechazar solicitud
     */
    async reject(
        req: Request,
        res: Response
    ) {

        try {

            const approval =
                await service.reject(

                    req.params.id as string,

                    req.body.approvedBy,

                    req.body.comments

                );

            res.json(
                approval
            );

        }
        catch (error) {

            res.status(500).json({
                message:
                    "Error al rechazar solicitud",
                error
            });

        }

    }

    /**
     * Buscar por estado
     */
    async getByStatus(
        req: Request,
        res: Response
    ) {

        try {

            const approvals =
                await service.getByStatus(
                    req.params.status as string
                );

            res.json(
                approvals
            );

        }
        catch (error) {

            res.status(500).json({
                message:
                    "Error al buscar aprobaciones",
                error
            });

        }

    }

    /**
     * Buscar por tipo de referencia
     */
    async getByReferenceType(
        req: Request,
        res: Response
    ) {

        try {

            const approvals =
                await service.getByReferenceType(
                    req.params.referenceType as string 
                );

            res.json(
                approvals
            );

        }
        catch (error) {

            res.status(500).json({
                message:
                    "Error al buscar aprobaciones",
                error
            });

        }

    }

    /**
     * Eliminar aprobación
     */
    async delete(
        req: Request,
        res: Response
    ) {

        try {

            const approval =
                await service.delete(
                    req.params.id as string
                );

            if (!approval) {

                return res.status(404).json({
                    message:
                        "Aprobación no encontrada"
                });

            }

            res.json({
                message:
                    "Aprobación eliminada correctamente"
            });

        }
        catch (error) {

            res.status(500).json({
                message:
                    "Error al eliminar aprobación",
                error
            });

        }

    }

}