import { Request, Response} from "express";
import { LPGService } from "./lpg.service";


const service = new LPGService();

export class LPGController{

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

        try {
            const requests = await service.getAll();
            res.json(requests);
        } catch(error) {

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

    async getById(
            req:Request,
            res:Response
        ){
            try {
                const request = 
                await service.getById(
                    req.params.id as string
                );
    
                if(!request) {
                    return res.status(404).json({
                        message:
                        "Requisición no encontrada"
                    });
                }
    
                res.json(request);
            }
    
            catch (error) {
                res.status(500).json({
                    message:"Error al obtener requisicion",
                    error
                });
            }
        }

    //Crear requisición

    async create(
        req:Request,
        res:Response
    ){
        try {
            const request = 
            await service.create(
                req.body
            );

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

        }
        catch (error) {
            res.status(500).json({
                message:"Error al crear requisición", 
                error
            });
        }
    }   
    
    
    //Actualizar requisición
    async update(
            req:Request,
            res:Response
        ){
            try {
    
                const request = await service.update(
                    req.params.id as string,
                    req.body
                );
    
                if (!request) {
                    return res.status(404).json({
                        message:
                        "Requisición no encontrada"
                    });
                }
    
                res.json(request);
            }catch(error) {
                res.status(500).json({
                    message:"Error al actualizar requisición",
                    error
                });
            }
        }
    
        //Eliminar requisición
    
        async delete(
            req:Request,
            res:Response
        ){
            try{
                const request = 
                await service.delete(
                    req.params.id as string
                );
    
                if(!request){
                    return res.status(404).json({
                        message:
                        "Requisición no encontrada"
                    });
                }
                res.json({
                    message:"Requisición eliminada correctamente"
                });
            }
            catch (error) {
                res.status(500).json({
                    message: "Error al eliminar requisición",
                    error
                });
            }
        }
}