import {
  Injectable,
  CanActivate,
  ExecutionContext,
  Logger,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { PrismaService } from '../prisma/prisma.service';

@Injectable()
export class AdminGuard implements CanActivate {
  private readonly logger = new Logger(AdminGuard.name);

  constructor(private prisma: PrismaService) {}

  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    const request = context.switchToHttp().getRequest();
    return this.validateRequest(request);
  }

  async validateRequest(request: any): Promise<boolean> {
    const user = await this.prisma.user.findUnique({
      where: { id: request.user.userId },
    });
    return user?.isAdmin === true;
  }
  // async validateRequest(request: any): Promise<boolean> {
  //   this.logger.debug(`Validating request for user ID: ${request.user.userId}`);
  //   const user = await this.prisma.user.findUnique({
  //     where: { id: request.user.userId },
  //   });
  //   const isAdmin = user?.isAdmin === true;
  //   this.logger.debug(`User is admin: ${isAdmin}`);
  //   return isAdmin;
  // }
}
