import { Injectable } from '@nestjs/common';
import { EntityPermissions, normalizeRoles, hasAnyRole } from '../../core/entity-permissions.base';

@Injectable()
export class DealPermissions extends EntityPermissions {
  private readonly WRITE_ROLES = ['super_admin', 'club_admin', 'manager'];

  private readonly ADMIN_ROLES = ['super_admin', 'club_admin'];

  canCreate(_userId: string, role: string | string[]): boolean {
    return hasAnyRole(normalizeRoles(role), this.WRITE_ROLES);
  }

  canRead(
    _userId: string,
    role: string | string[],
    _clubId: string | null,
    _entityClubId: string | null,
  ): boolean {
    const roles = normalizeRoles(role);
    if (hasAnyRole(roles, this.ADMIN_ROLES)) return true;
    if (roles.includes('manager')) return true; // filtered by assigneeId
    return false;
  }

  canUpdate(userId: string, role: string | string[], _clubId: string | null, entity: any): boolean {
    const roles = normalizeRoles(role);
    if (hasAnyRole(roles, this.ADMIN_ROLES)) return true;
    if (roles.includes('manager')) return entity.assigneeId === userId;
    return false;
  }

  canDelete(_userId: string, role: string | string[]): boolean {
    return hasAnyRole(normalizeRoles(role), this.ADMIN_ROLES);
  }

  filterByRole(
    userId: string,
    role: string | string[],
    _clubId: string | null,
  ): Record<string, any> {
    const roles = normalizeRoles(role);
    if (hasAnyRole(roles, this.ADMIN_ROLES)) return {};
    if (roles.includes('manager')) {
      return { assigneeId: userId };
    }
    return {};
  }
}
