import { Request, Response } from "express";
import { FuelPriceService } from "./fuelPrices.service";

const fuelPriceService = new FuelPriceService();

export class FuelPriceController {

        async getAll(
            req:Request,
            res:Response
        ){
            try{
                const fuelPrices = await fuelPriceService.getAll();
        
                res.json(fuelPrices);

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

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

                if(!fuelPrice){
                    return res.status(404).json({
                        message:"Precio de combustible no encontrado"
                    });
                }

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

            }
        }

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

            try{
                const fuelPrice = await fuelPriceService.create(
                    req.body
                );

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

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

            try{
                const fuelPrice = await fuelPriceService.update(
                    req.params.id as string,
                    req.body
                );
                
                if(!fuelPrice){
                    return res.status(404).json({
                        message:"Precio de combustible no encontrado"
                    });
                }

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

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

                if(!fuelPrice){
                    return res.status(404).json({
                        message:"Precio de combustible no encontrado"
                    });
                }

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

            }
        }
}
