import fuelRequestModel from "../fuel-requests/fuelRequest.model";
import externalInvoiceModel from "../external-invoices/externalInvoice.model";
import lpgModel from "../lpg/lpg.model";
import approvalModel from "../approvals/approval.model";
import anomalyModel from "../anomalies/anomaly.model";
import pumpReadingModel from "../pump-reading/pumpReading.model";


export class ReportService{

    async getSummary(){
        //Total de requisiciones
        const fuelRequests = await fuelRequestModel.countDocuments();

        //Total de facturas externas
        const externalInvoices = await externalInvoiceModel.countDocuments();

        //Total de requisiciones LPG
        const lpgRequests = await lpgModel.countDocuments();

        //Total de galones solicitados
        const pendingApprovals = await approvalModel.countDocuments({
            status:"PENDIENTE"
        });

        //Total de anomalías abiertas
        const openAnomalies = await anomalyModel.countDocuments({
            status:"ABIERTA"
        })

        //Tootal de lecturas de bomba
        const pumpReadings = await pumpReadingModel.countDocuments();

        const gallons = await fuelRequestModel.aggregate([
            {
                $group:{
                    _id:null,
                    totalGallons:{
                        $sum: "$gallonQuantity"
                    }
                }
            }
        ]);

        //Total invertido
        const totalCost = await fuelRequestModel.aggregate([
            {
                $group:{
                    _id:null,
                    totalAmount:{
                        $sum: "$totalAmount"
                    }
                }
                
            }
        ]);

        return{
            fuelRequests,
            externalInvoices,
            lpgRequests,
            pendingApprovals,
            openAnomalies,
            pumpReadings,

            totalGallons:
                gallons.length > 0
                ? gallons[0].totalGallons
                :0,

            
            totalCost:
                totalCost.length > 0
                ? totalCost[0].totalAmount
                :0

        }
    }

    async getFuelRequestReport(){

        return await fuelRequestModel
        .find()
        .populate("fuelPumpId")
        .populate("fuelTypeId")
        .populate("createdBy")
        .populate("authorizedBy")
        .sort({
            createdAt:-1
        });
    }

    async getExternalInvoiceReport(){

        return await externalInvoiceModel
        .find()
        .populate("fuelTypeId")
        .populate("createdBy")
        .populate("authorizedBy")
        .sort({
            invoiceDate:-1
        });
    }

    async getLpgReport(){

        return await lpgModel
        .find()
        .populate("requestedBy")
        .populate("approvedBy")
        .sort({
            requestDate:-1
        });
    }

    async getAnomaliesReport(){

        return await anomalyModel
        .find()
        .populate("reportedBy")
        .populate("resolvedBy")
        .sort({
            createdAt:-1
        });
    }

    async getPumpReadingsReport(){

        return await pumpReadingModel
        .find()
        .populate("fuelPumpId")
        .populate("fuelTypeId")
        .populate("createdBy")
        .sort({
            readingDate:-1
        });
    } 

    async getVehicleReport(
        vehicleId:number
    ){

        return await fuelRequestModel
        .find({
            vehicleId
        })
        .populate("fuelPumpId")
        .populate("fuelTypeId")
        .sort({
            createdAt:-1
        });
    } 

    async getEmployeeReport(
        employeeId:number
    ){

        return await fuelRequestModel
        .find({
            employeeId
        })
        .populate("fuelPumpId")
        .populate("fuelTypeId")
        .sort({
            createdAt:-1
        });
    } 

    async getDepartmentReport(
        departmentId:number
    ){

        return await fuelRequestModel
        .find({
            departmentId
        })
        .populate("fuelPumpId")
        .populate("fuelTypeId")
        .sort({
            createdAt:-1
        });
    }
    
    async getRouteReport(
        routeId:number
    ){

        return await fuelRequestModel
        .find({
            routeId
        })
        .populate("fuelPumpId")
        .populate("fuelTypeId")
        .sort({
            createdAt:-1
        });
    }   
}