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

const WRITE_ROLES = ['super_admin', 'club_admin', 'manager'];
const ADMIN_ROLES = ['super_admin', 'club_admin'];

@Injectable()
export class TrainingPermissions extends EntityPermissions {
  canCreate(_userId: string, role: string | string[]): boolean {
    return hasAnyRole(normalizeRoles(role), WRITE_ROLES);
  }

  canRead(): boolean {
    return true;
  }

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

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

  filterByRole(
    userId: string,
    role: string | string[],
    clubId?: string | null,
  ): Record<string, any> {
    const roles = normalizeRoles(role);
    if (roles.includes('super_admin')) return {};
    if (roles.includes('club_admin')) return { clubId };
    // Trainers see their own
    if (roles.includes('trainer')) return { trainerId: userId };
    // Managers see club trainings
    return { clubId };
  }
}
