import { Injectable } from '@angular/core';
import {
  CanActivate,
  ActivatedRouteSnapshot,
  RouterStateSnapshot,
  UrlTree, Router
} from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from '../services/auth.service';

@Injectable({
  providedIn: 'root'
})
export class RoleGuard implements CanActivate {

  constructor(
    private authService: AuthService,
    private router: Router,
  ) {}

  canActivate(
    routeSnapshot: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    const expectedRole: string[] = routeSnapshot.data.expectedRole;
    if (expectedRole.includes(this.authService.getUser().userInfo?.rightsId.toString())) {
      console.log('You have enough rights!');
     return true;
    }
    console.log('You are not allowed to be here!');
    this.router.navigate(['']);
    return false;
  }
}
