import { inject } from '@angular/core';
import { AccountService } from 'app/core/auth/account.service';
import { Resources } from 'app/security/resources.model';
import { SecurityAction } from 'app/security/security-action.model';

export abstract class ResilientBaseComponent {
  protected accountService = inject(AccountService);

  Resources = Resources; // ✅ This makes the enum accessible in the template

  /* 
   * Security Permission methods. Evaluates if the current authenticated user has PERMISSION to do the action 
   * In html template, if security permission fails, the secured element is hidden.
  */
  hasDeletePermission(evalResource: string): boolean {
    return this.accountService.evalPermission(evalResource, SecurityAction.DELETE);
  }
  
  hasUpdatePermission(evalResource: string): boolean {
    return this.accountService.evalPermission(evalResource, SecurityAction.UPDATE);
  }
  
  hasCreatePermission(evalResource: string): boolean {
    return this.accountService.evalPermission(evalResource, SecurityAction.CREATE);
  }
  
  hasReadPermission(evalResource: string): boolean {
    return this.accountService.evalPermission(evalResource, SecurityAction.READ);
  }

  /* 
   * Action Permission methods. Evaluates if the current authenticated user AND/OR 
   * the current Entity instance allows the action to be done. Example, the User hasPermission to DELETE, 
   * but the Entity instance is in a state that DOESN'T allow it.
   * In html template, if action permission fails, the element is showned but disabled
  */
  canCreate(): boolean { return true; }
  canRead(): boolean { return true; }
  canUpdate(): boolean { return true; }
  canDelete(): boolean { return true; }
}