import {
  CanActivate,
  ExecutionContext,
  ForbiddenException,
  HttpStatus,
  Injectable,
} from '@nestjs/common';
import AppResponse from '../response/ktq-response';
import { SystemLang } from '../lang/system.lang';

@Injectable()
export class SystemAccountGuard implements CanActivate {
  canActivate(context: ExecutionContext): boolean {
    const request = context.switchToHttp().getRequest();

    if (!request.admin || !request.admin.is_system_account) {
      throw new ForbiddenException(
        AppResponse.toResponse(null, {
          message: SystemLang.getText('messages', 'forbidden'),
          status_code: HttpStatus.FORBIDDEN,
        }),
      );
    }

    return true;
  }
}
