// models/userModel.js
const mongoose = require("mongoose");
require('../models/roleModel')

const userSchema = new mongoose.Schema(
  {
    firstName: {
      type: String,
      required: [true, "First name is required"],
      trim: true,
    },
    lastName: {
      type: String,
      trim: true,
    },
    fatherName: {
      type: String,
      trim: true,
      default: "-"

    },
    dateOfBirth: {
      type: String,
      trim: true,
      default: "-"
    },
    email: {
      type: String,
      required: [true, "Email is required"],
      unique: true,
      lowercase: true,
      trim: true,
    },
    aadharNumber: {
      type: String,
      trim: true,
      default: "-",
      // match: [/^\d{10,15}$/, "Please enter a valid Aadhar number"],
    },
    panNumber: {
      type: String,
      trim: true,
      default: "-",
    },
    maritalStatus: {
      type: String,
      trim: true,
      default: "-",
    },
    bloodGroup: {
      type: String,
      trim: true,
      default: "-",
    },
    emergencyContact: {
      type: String,
      trim: true,
      default: "-",
    },
    idProofType: {
      type: String,
      trim: true,
      default: "-",
    },
    joinedDate: {
      type: String,
      trim: true,
      default: "-",
    },

    relievedDate: {
      type: String,
      trim: true,
      default: "-",
    },
    password: {
      type: String,
      required: [true, "Password is required"],
      minlength: [6, "Password must be at least 6 characters long"],
    },
    phone: {
      type: String,
      trim: true,
      match: [/^\d{10,15}$/, "Please enter a valid phone number"],
    },
    role: {
      type: String,
      enum: ["superadmin", "admin", "manager", "staff", "agent", "user", "doctor","security", "qc", "warehouse"],
      default: "user",
    },
    orgId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Organization",
      required: [true, "Organization ID is required"],
    },
    salary: {
      type: Number,
      required: false, //later need to make true 
      default: null
    },

    workingDays: {
      type: Number,
      default: 30,
    },
    specializations: {
      type: [
        {
          type: mongoose.Schema.Types.ObjectId,
          ref: "Specialization",
        },
      ],
      default: [],   // ✅ THIS IS MISSING
    },

   

    workingHours: {
      start: {
        type: String, // "09:00"
        // required: true,
        default: "09:30"
      },
      end: {
        type: String, // "17:00"
        // required: true,
        default: "18:30"
      },
    },

    // below fields are related to doctor timings 
    lunchBreak: {
      start: { type: String, default: "13:30" },
      end: { type: String, default: "14:30" },
    },

    slotDuration: {
      type: Number,
      default: null, // minutes
    },

    employeeType: {
      type: String,
      enum: ["full-time", "part-time", "contract"],
      default: "full-time",
    },

    reportingManager: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      default: null,
    },
    
    department: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Department",
      default: null,
    },
    designation: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Designation",
      default: null,
    },

    isAgent: {
      type: Boolean,
      default: false
    },
 
    status: {
      type: String,
      enum: ["Pending", "Approved", "Rejected"],
      default: "Approved",
    },
    createdBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      default: null,
    },
    updatedBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      default: null,
    },

    isDeleted: {
      type: Boolean,
      default: false, // soft delete flag
    },
    resetPasswordToken: {
      type: String
    },

    otp: {
      type: String, // hashed OTP
      default: null,
    },

    otpExpiresAt: {
      type: Date,
      default: null,
    },



  
    shift: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Shift",
      required: false, // keep optional
      default: null,
    }
    ,
    address: {
      street: { type: String, trim: true },
      city: { type: String, trim: true },
      state: { type: String, trim: true },
      country: { type: String, trim: true },
      zip: { type: String, trim: true },
    },
    profileImage: {
      type: String,
      default: null,
    },
    lastLogin: {
      type: Date,
      default: null,
    },

    // ✅ New device-related fields
    deviceId: {
      type: String,
      default: null,
    },
    deviceModel: {
      type: String,
      default: null,
    },
    appVersion: {
      type: String,
      default: null,
    },
    deviceToken: {
      type: String,
      default: null,
    },
    platform: {
      type: String,
      enum: ["android", "ios", "web"],
      required: false,
    },


   
    gender: {
      type: String,
      enum: ["male", "female", "other"],
      default: "other",
      trim: true,
    },

  },
  {
    timestamps: true,
  }
);

module.exports = mongoose.model("User", userSchema);
