// Connections
import mongoConnection from "$connections/mongo/mongo.connection.js";

import mongoose from "mongoose";
const mongooseSchema = mongoose.Schema;

export const schemaModel = {
  label: {
    type: String,
    default: "",
  },
  value: {
    type: String,
    default: "",
  },
  permissions: {
    type: [String],
    default: [],
  },
  isSuperAdmin: {
    type: Boolean,
    default: false,
  },
};

export const schema = new mongooseSchema(schemaModel, { timestamps: true });

// Method to check if role has a permission
schema.methods.hasPermission = function (permissionKey) {
  if (this.isSuperAdmin) return true;

  // Check exact match
  return this.permissions.includes(permissionKey);
};

export default mongoConnection.model("Role", schema);
