const mongoose = require('mongoose');

// Define the User Schema
const userSchema = new mongoose.Schema({
  username: {
    type: String,
    required: true,
    unique: true,
    trim: true,
  },
  email: {
    type: String,
    required: true,
    unique: true, // Ensure email is unique for each user
    trim: true,
   // match: [/.+\@.+\..+/, 'Please enter a valid email address'], // Basic email validation
  },
  passcode: {
    type: String,
    unique: true,
    required: true,
  },
  password: {
    type: String,
    required: true,
  },


  roleId: {
    type: mongoose.Schema.Types.ObjectId, // Reference to the Roles collection
    ref: 'Role',
    required: true},


  companyId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Company', // Assuming you have a Company model
    required: true,
  },

  branchId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Branch', // Assuming you have a Branch model
    required: true,
  },
  kitchenId: { type: mongoose.Schema.Types.ObjectId, ref:'Kitchen' ,
  required: false},

fcmToken: {

   type: String,
   default: '',
  
},
resetPasswordToken: {

  type: String,
  default: '',
 
},
resetPasswordExpires :{
  type: Date,
  default: null,
 },


  allowedBranchId: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Branch', // Allow multiple branches
    },
  ],
}, { timestamps: true });

// Create the User model
const User = mongoose.model('User', userSchema);

module.exports = User;

