import { Injectable } from '@nestjs/common';
import { CaslAbilityFactory } from '../authorization/casl-ability.factory';
import { CaslAction, SubjectName } from '../authorization/casl.constants';
import type { AgentToolContext } from './agent-tool.types';

@Injectable()
export class AgentBusinessScopeFactory {
  constructor(private readonly abilityFactory: CaslAbilityFactory) {}

  private ability(context: AgentToolContext) {
    return this.abilityFactory.createForUser({
      permissions: context.permissions,
      isSuperAdmin: context.isSuperAdmin,
    });
  }

  canManageAllClasses(context: AgentToolContext): boolean {
    const ability = this.ability(context);
    return context.isSuperAdmin || ability.can(CaslAction.Update, SubjectName.Class);
  }

  canManageAllAttendance(context: AgentToolContext): boolean {
    const ability = this.ability(context);
    return (
      context.isSuperAdmin ||
      ability.can(CaslAction.Manage, SubjectName.Attendance) ||
      ability.can(CaslAction.Update, SubjectName.Class)
    );
  }

  canManageAllDashboard(context: AgentToolContext): boolean {
    const ability = this.ability(context);
    return (
      context.isSuperAdmin ||
      ability.can(CaslAction.Manage, SubjectName.Dashboard) ||
      ability.can(CaslAction.Update, SubjectName.Class)
    );
  }
}
