import mongoose from "mongoose";

const externalInvoiceSchema = new mongoose.Schema({

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

    zone:{
        type:String,
        required:true,
        enum:[
            "Ocotepeque",
            "Gracias",
            "Santa Bárbara"
        ]
    },

    employeeId:{
        type:Number,
        required:true
    },

    vehicleId:{
        type:Number,
        required:true
    },

    routeId:{
        type:Number, 
    },

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

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

    gallons:{
        type:Number,
        required:true
    },

    pricePerGallon:{
        type:Number,
        required:true
    },

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

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

    approvedBy:{
        type:mongoose.Schema.Types.ObjectId,
        ref:"User"
    },

    comments:{
        type:String
    },

    status:{
        type:String,
        enum:[
            "PENDIENTE",
            "APROBADO",
            "RECHAZADO"
        ],
        default:"PENDIENTE"
    }

},
{
    timestamps:true,
    versionKey:false
});

//Calcular total antes de guardar

externalInvoiceSchema.pre(
    "save",
    async function () {

        this.totalAmount =
            this.gallons *
            this.pricePerGallon;

    }
);

export default mongoose.model(
    "ExternalInvoice",
    externalInvoiceSchema
);