const mongoose = require("mongoose");

const permissionSchema = new mongoose.Schema(
  {
    module: {
      type: String,
      //   required: true,
      trim: true,
      index: true,
    },

    subModule: {
      type: String,
      default: null,
      trim: true,
      index: true,
    },

    view: { type: Boolean, default: false },
    add: { type: Boolean, default: false },
    edit: { type: Boolean, default: false },
    delete: { type: Boolean, default: false },
  },
  { _id: false },
);

const designationPermissionSchema = new mongoose.Schema(
  {
    designationId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Designation",
    //   required: true,
    //   unique: true, //  one permission document per designation
      index: true,
      default : null,
    },

    orgId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Organization",
      required: false,
      index: true,
    },

    role: {
      type: String,
      enum: [
        "superadmin",
        "admin",
        "manager",
        "staff",
        "agent",
        "user",
        "doctor",
        "security",
        "qc",
      ],
      required: true,
    //   unique: true, //  ONE permission document per ROLE
      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("Permission", designationPermissionSchema);
