import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class SuperAdminGuard implements CanActivate {
  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    console.log('SuperAdminGuard: Checking if user is SUPER_ADMIN');
    const req = context.switchToHttp().getRequest();
    const user = req.user as any;
    // console.log('SuperAdminGuard: User from request', user);
    if (!user) return false;
    const roleName = user?.role?.name ?? user?.role?.code ?? null;
    return roleName === 'SUPER_ADMIN';
  }
}
