import mongoose from "mongoose";

const pumpReadingSchema = new mongoose.Schema({

        readingNumber:{
            type:String,
            required:true,
            unique:true
        },

        fuelPumpId:{
            type:mongoose.Schema.Types.ObjectId,
            ref:"FuelPump",
            required:true
        },

        fuelTypeId:{
            type:mongoose.Schema.Types.ObjectId,
            ref:"FuelType",
            required:true
        },

        readingDate:{
            type:Date,
            required:true
        },

        initialReading:{
            type:Number,
            required:true,
            min:0
        },

        finalReading:{
            type:Number,
            required:true,
            min:0
        },

        gallonsDispensed:{
            type:Number,
            default:0
        },

        systemDispensed:{
            type:Number,
            default:0
        },

        difference:{
            type:Number,
            default:0
        },

        createdBy:{
            type:mongoose.Schema.Types.ObjectId,
            ref:"User",
            required:true
        },

        status:{
            type:String,
            enum:["ABIERTA",
                "CUADRADA",
                "CON_DIFERENCIA"
            ],
            default:"ABIERTA"
        
        },

        comments:{
            type:String
        }
},
{
    timestamps:true,
    versionKey:false
}

);

pumpReadingSchema.pre("save",
    function(){

        this.gallonsDispensed = this.finalReading-this.initialReading;
    }
);

export default mongoose.model("PumpReading",pumpReadingSchema);