import DB from "../database.js";
import { ModelDataBase } from "@hibas123/safe_mongo/lib/model.js";
import { ObjectId } from "mongodb";

export interface IPermission extends ModelDataBase {
   name: string;
   description: string;
   client: ObjectId;
   grant_type: "user" | "client";
}

const Permission = DB.addModel<IPermission>({
   name: "permission",
   versions: [
      {
         migration: () => { },
         schema: {
            name: { type: String },
            description: { type: String },
            client: { type: ObjectId },
         },
      },
      {
         migration: (old) => {
            old.grant_type = "user";
         },
         schema: {
            name: { type: String },
            description: { type: String },
            client: { type: ObjectId },
            grant_type: { type: String, default: "user" },
         },
      },
   ],
});

export default Permission;
