import { RolesBuilder } from 'nest-access-control';

export enum AppRoles {
  GUEST = 'GUEST',
  USER_MANAGER_OWN_PRODUCT = 'USER_MANAGER_OWN_PRODUCT',
  SYSTEM_ADMIIN = 'SYSTEM_ADMIN',
}

export const roles: RolesBuilder = new RolesBuilder();
roles
  .grant(AppRoles.GUEST)
  .readAny(
    ['product', 'user', 'media', 'post', 'comment', 'notice', 'log'],
    ['*', '!create_time', '!creator'],
  )
  .grant(AppRoles.USER_MANAGER_OWN_PRODUCT) // define new or modify existing role. also takes an array.
  .extend(AppRoles.GUEST)
  .createAny(['product', 'user', 'media', 'post', 'comment', 'notice', 'log']) // equivalent to .createOwn('product', ['*'])
  .updateOwn(['product', 'user', 'media', 'post', 'comment', 'notice', 'log'])
  .deleteOwn(
    ['product', 'user', 'media', 'post', 'comment', 'notice', 'log'],
    ['*', '!createdAt', '!update_time', '!published'],
  )
  .grant(AppRoles.SYSTEM_ADMIIN) // switch to another role without breaking the chain
  .createAny(['product', 'user', 'media', 'post', 'comment', 'notice', 'log'])
  .readAny(['product', 'user', 'media', 'post', 'comment', 'notice', 'log']) // inherit role capabilities. also takes an array
  .updateAny(['product', 'user', 'media', 'post', 'comment', 'notice', 'log']) // explicitly defined attributes
  .deleteAny(['product', 'user', 'media', 'post', 'comment', 'notice', 'log']);
