import mongoose, { Schema } from "mongoose";

const serviceSchema= new Schema({
    customerName:String,
    customerPhone:String,
    vehicleNumber:String,
    vehicleType:{type:String,enum:['bike','car'],default:'bike'},
    serviceType:{type:String,enum:['repair','maintenance','inspection'],default:'repair'},
    userId:{
        type:mongoose.Schema.Types.ObjectId,
        ref:'User',
        required:true
    },
    invoiceId: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Invoice',
        },
    status:{type:String,enum:['pending','in-progress','ready','delivered'],default:'pending'},
},{timestamps:true})
serviceSchema.pre('save',async function () {
    if (!this.customId) {
        let isUnique = false;
        let newId;

        while (!isUnique) {
            newId = Math.floor(100000 + Math.random() * 900000).toString(); // ✅ 6-digit

            const existing = await this.constructor.findOne({ customId: newId });
            if (!existing) {
                isUnique = true;
            }
        }

        this.customId = newId;
    }
})
const Service=mongoose.model('Service',serviceSchema)
export default Service