import { Request, Response } from "express";
import { AuditLogService } from './auditLog.service';

const service = new AuditLogService();

export class AuditLogController{

    async create(
        req:Request,
        res:Response
    ){

        try{

            const auditLog = await service.create(
                req.body
            )
            res.status(201).json(auditLog);

        }catch(error){

            res.status(500).json({
                message:"Error al crear el registro de auditoría",
                error
            });

        }
    }

    async getAll(
        req:Request,
        res:Response
    ){
        try{

            const auditLog = await service.getAll()
            res.status(200).json(auditLog);

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

            });
        }
    }

    async getById(
        req:Request,
        res:Response
    ){
        try{

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

            if(!auditLog){
                return res.status(404).json({
                    message:"Registro no encontrado"
                });
            }

            res.status(200).json(auditLog);

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

        }

    }

    async getByUser(
        req:Request,
        res:Response
    ){
        try{
        const auditLog = await service.getByUser(
            req.params.userId as string
        );

        res.status(200).json(auditLog);
        } catch(error){
            res.status(500).json({
                message:"Error al obtener registros",
                error
            });
        }
    }

    async getByModule(
        req:Request,
        res:Response
    ){
        try{

            const auditLog = await service.getByModule(
                req.params.module as any
            );

        res.status(200).json(auditLog);

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

        }
    }

    async getByAction(
        req:Request,
        res:Response
    ){
        try{

            const auditLog = await service.getByAction(
                req.params.action as any
            );
        
            res.status(200).json(auditLog);

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

        }
    }

    async getByStatus(
        req:Request,
        res:Response
    ){
        try{


            const auditLog = await service.getByStatus(
                req.params.status as any
            );

            res.status(200).json(auditLog);

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

        }
    }

    async getByDateRange(
        req:Request,
        res:Response
    ){
        try{

            const {startDate, endDate} = req.query;

            const auditLog = await service.getByDateRange(
                new Date(startDate as string),
                new Date(endDate as string)
            );

            res.status(200).json(auditLog)

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

    async delete(
        req:Request,
        res:Response
    ){
        try{

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

            if(!auditLog){
                return res.status(404).json({
                    message:"Registro no encontrado"
                })
            }
            res.status(200).json(auditLog);

        }catch(error){
            res.status(500).json({
                message:"Error al eliminar registros",
                error
            });
        }
    }
}