import * as plugins from '../plugins.js';
import { OrganizationManager } from './classes.organizationmanager.js';
import { User } from './classes.user.js';

@plugins.smartdata.Manager()
export class Organization extends plugins.smartdata.SmartDataDbDoc<
  Organization,
  plugins.idpInterfaces.data.IOrganization,
  OrganizationManager
> {
  public static async createNewOrganizationForUser(
    organizationManagerArg: OrganizationManager,
    userIdArg: string,
    orgNameArg: string,
    slugNameArg: string,
  ) {
    const newOrg = new Organization();
    newOrg.id = plugins.smartunique.shortId();
    newOrg.data = {
      name: orgNameArg,
      slug: slugNameArg,
      billingPlanId: null,
      roleIds: [],
    }
    await newOrg.save();
    return newOrg;
  }

  // INSTANCE
  @plugins.smartdata.unI()
  id: plugins.idpInterfaces.data.IOrganization['id'];
  
  @plugins.smartdata.svDb()
  data: plugins.idpInterfaces.data.IOrganization['data'];

  public async checkIfUserIsAdmin(userArg: User) {
    const role = await this.manager.receptionRef.roleManager.getRoleForUserAndOrg(userArg, this);
    return role.data.role === 'admin';
  }
}
