import { SetMetadata } from '@nestjs/common';
import { Action, Subjects } from '../abilities/ability.factory';
import { CHECK_ABILITY, RequiredPermission } from '../guards/abilities.guard';

/**
 * Decorator to check CASL abilities on a route
 *
 * @example
 * @CheckAbilities({ action: Action.Create, subject: 'VIP' })
 * async create(@Body() dto: CreateVIPDto) {
 *   return this.service.create(dto);
 * }
 *
 * @example Multiple permissions (all must be satisfied)
 * @CheckAbilities(
 *   { action: Action.Read, subject: 'VIP' },
 *   { action: Action.Update, subject: 'VIP' }
 * )
 */
export const CheckAbilities = (...permissions: RequiredPermission[]) =>
  SetMetadata(CHECK_ABILITY, permissions);

/**
 * Helper functions for common permission checks
 */
export const CanCreate = (subject: Subjects) =>
  CheckAbilities({ action: Action.Create, subject });

export const CanRead = (subject: Subjects) =>
  CheckAbilities({ action: Action.Read, subject });

export const CanUpdate = (subject: Subjects) =>
  CheckAbilities({ action: Action.Update, subject });

export const CanDelete = (subject: Subjects) =>
  CheckAbilities({ action: Action.Delete, subject });

export const CanManage = (subject: Subjects) =>
  CheckAbilities({ action: Action.Manage, subject });
