import { Request, Response} from "express";
import { FuelPumpService } from "./fuelPump.service";

const fuelPumpService = new FuelPumpService();

export class FuelPumpController {

    async getAll(
        req:Request,
        res:Response
    ){
        try{
            const fuelPumps = await fuelPumpService.getAll();

            res.json(fuelPumps);
        }catch(error){
            res.status(500).json({
                message:"Error al obtener bomba de combustible",
                error
            })

        }
    }

    async getById(
        req:Request,
        res:Response
    ){
        try{
            const fuelPump = await fuelPumpService.getById(
                req.params.id as string
            );

            if(!fuelPump){
                return res.status(404).json({
                    message:"Bomba combustible no encontrada"
                });
            }
            
            res.json(fuelPump)

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

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

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

        }catch(error){
            res.status(500).json({
                message:"Error al crear bomba de combustible",
                error
            });

        }
    }

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

        try{
            const fuelPump = await fuelPumpService.update(
                req.params.id as string,
                req.body
            );

            if(!fuelPump){
                return res.status(404).json({
                    message:"Bomba de combustible no encontrada"
                })
            }

            res.status(200).json(fuelPump);
        }catch(error){
            res.status(500).json({
                message:"Error al actualizar bomba de combustible",
                error
            })
        }
    }

    async delete(
        req:Request,
        res:Response
    ){
        try{
            const fuelPump = await fuelPumpService.delete(
                req.params.id as string
            );

            if(!fuelPump){
                    return res.status(404).json({
                        message:"Bomba de combustible no encontrada"
                    })
                }

            res.json({
                message:"Bomba de combustible eliminada correctamente"
            });

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