import {Request, Response } from "express";
import { PumpReadingService } from "./pumpReading.service";


const service = new PumpReadingService();

export class PumpReadingController {

    async create(
        req:Request,
        res:Response
    ){
        try {
            const reading = await service.create(
                req.body
            )
            res.status(201).json(reading);
        }catch (error){
            res.status(500).json({
                message:"Error al crear la lectura",
                error
            });
        }

    }

    async getAll(
        req:Request,
        res:Response
    ){
        try{
            const readings = await service.getAll();
            res.json(readings);
        }catch(error){
            res.status(500).json({
            message:"Error al obtener las lecturas",
            error
            });
        }
    }

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

            const reading = await service.getById(
                req.params.id as string 
            )
            if(!reading){
                return res.status(404).json({
                    message:"Lectura no encontrada"
                });
            }
            res.json(reading);
        }catch(error){
            res.status(500).json({
                message:"Error al obtener la lectura",
                error
            });
        }
    }

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

            const reading = await service.update(
                req.params.id as string,
                req.body
            );

            if(!reading){
                return res.status(404).json({
                    message:"Lectura no encontrada"
                });
            }
            res.json(reading);
        

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

        }
    }

    async delete(
        req:Request,
        res:Response
    ){
        try{
            const reading = await service.delete(
                req.params.id as string
            );
             if(!reading){
                return res.status(404).json({
                    message:"Lectura no encontrada"
                });
            }
            res.json({
                message:"Lectura eliminada"
            });

        }catch (error){
            res.status(500).json({
                message:"Error al eliminiar lectura"
            });
        }
    }

    async calculateDispensedGallons(

        req: Request,

        res: Response

    ) {

        try {

            const gallons =

                await service.calculateDispensedGallons(

                    req.params.id as string

                );

            res.json({

                gallonsDispensed: gallons

            });

        } catch (error) {

            res.status(500).json({

                message: "Error al calcular galones despachados.",

                error

            });

        }

    }

    // Calcular galones registrados por el sistema


    async calculateSystemDispensed(

        req: Request,
        res: Response

    ) {

        try {

            const gallons = await service.calculateSystemDispensed(

                    req.params.id as string

                );

            res.json({

                systemDispensed: gallons

            });

        } catch (error) {

            res.status(500).json({

                message: "Error al calcular galones del sistema.",
                error

            });

        }

    }


    // Calcular diferencia

    async calculateDifference(

        req: Request,
        res: Response

    ) {

        try {

            const difference = await service.calculateDifference(

                    req.params.id as string

                );

            res.json({

                difference

            });

        } catch (error) {

            res.status(500).json({

                message: "Error al calcular la diferencia.",

                error

            });

        }

    }

    // Actualizar estado

    async updateStatus(

        req: Request,
        res: Response

    ) {

        try {

            const status = await service.updateStatus(

                    req.params.id as string

                );

            res.json({

                status

            });

        } catch (error) {

            res.status(500).json({

                message: "Error al actualizar el estado.",
                error

            });

        }

    }

    //Actualizar Fuel Request relacionadas


    async updateFuelRequests(

        req: Request,
        res: Response

    ) {

        try {

            const requests = await service.updateFuelRequests(

                    req.params.id as string

                );

            res.json(requests);

        } catch (error) {

            res.status(500).json({

                message: "Error al actualizar las Fuel Request.",
                error

            });

        }

    }

    //Crear anomalía por diferencia

    async createDifferenceAnomaly(

        req: Request,
        res: Response

    ) {

        try {

            const anomaly = await service.creataDifferenceAnomaly(

                    req.params.id as string

                );

            res.json(anomaly);

        } catch (error) {

            res.status(500).json({

                message: "Error al crear la anomalía.",

                error

            });

        }

    }

    //Cerrar lectura

    async closeReading(

        req: Request,
        res: Response

    ) {

        try {

            const reading = await service.closeReading(

                    req.params.id as string

                );

            res.json(reading);

        } catch (error) {

            res.status(500).json({

                message: "Error al cerrar la lectura.",

                error

            });

        }

    }
}