// models/Role.js

const mongoose = require("mongoose");

const permissionSchema = new mongoose.Schema({
  module: {
    type: String,
    required: true,
    // default : null,
  },
  subModule: {
    type: String,
    default: null, // null means module-level permission
  },
  view: { type: Boolean, default: false },
  add: { type: Boolean, default: false },
  edit: { type: Boolean, default: false },
  delete: { type: Boolean, default: false },
});

const roleSchema = new mongoose.Schema({
  // name: String,
  name: {
    type: String,
    required: true,
    unique: true,
    enum: [
      "superadmin",
      "admin",
      "manager",
      "staff",
      "agent",
      "user",
      "doctor",
    ],
    // lowercase: true,
    // trim: true,
    // index: true,
  },
  permissions: {
    type: [permissionSchema],
    default: [],
  },

    isActive: {
      type: Boolean,
      default: true,
      index: true,
    },

    createdBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      default: null,
    },

    updatedBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      default: null,
    },

}, { timestamps: true });

module.exports = mongoose.model("Role", roleSchema);
