import { SetMetadata } from '@nestjs/common';
import { PolicyHandler } from '../interfaces';

export const CHECK_POLICIES_KEY = 'check_policies';

/**
 * Declare CASL-based policy requirements on a route handler or controller.
 *
 * Handlers are evaluated with AND semantics — every handler must pass
 * for the request to be allowed.
 *
 * ### Usage — callback handler
 * ```ts
 * @CheckPolicies((ability) => ability.can('read', 'Student'))
 * ```
 *
 * ### Usage — class-based handler (prefer this for testability)
 * ```ts
 * import { IPolicyHandler } from '../interfaces';
 *
 * class ReadStudentPolicyHandler implements IPolicyHandler {
 *   handle(ability: AppAbility) {
 *     return ability.can('read', 'Student');
 *   }
 * }
 *
 * @CheckPolicies(new ReadStudentPolicyHandler())
 * ```
 */
export const CheckPolicies = (...handlers: PolicyHandler[]) =>
  SetMetadata(CHECK_POLICIES_KEY, handlers);
