import mongoose, { Schema } from "mongoose";

const employeeSchema = new Schema({
    customId: String,
    name: String,
    mobileNumber: String,
    email:String,
    joinDate:String,
    salary:Number,
    position:String,
    email: String,
    userId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true,
    },
    status:{type:String,enum:['active','inactive'],default:'active'}
}, { timestamps: true })
employeeSchema.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 Employee = mongoose.model('Employee', employeeSchema);
export default Employee;