import { Request, Response } from "express";
import { FuelTypeService } from "./fuelType.service";


const fuelTypeService = new FuelTypeService();

export class FuelTypeController {

    //Obtener los datos de combustible

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

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

    //Obtener tipo de combustible por ID

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

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

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

    //Crear tipo de combustible
    async create(
        req:Request,
        res:Response
    ) {
        try{
            const fuelType = await fuelTypeService.create(
                req.body
            );

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

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

    //Actualizar tipo de combustible
    async update(
        req:Request,
        res:Response
    ){

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

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

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

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

        }
    }

    //Eliminar tipo de combustible
    async delete (
        req:Request,
        res:Response
    ){

        try{
            const fuelType = await fuelTypeService.delete(
                req.params.id as string
            );

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

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