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

@Injectable({
  providedIn: 'root'
})
export class AgenteReservaGuard implements CanActivate, CanLoad {
  constructor(private authService: AuthService,private router: Router){}
  
  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
      if (this.authService.verificarAuth() && this.authService.getUserRole() == 'agente') {
        return true;
      }
      this.router.navigate(['auth/login']);
      return false;
  }
  canLoad(
    route: Route,
    segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean {
      if (this.authService.verificarAuth() && this.authService.getUserRole() == 'agente') {
        return true;
      }
      this.router.navigate(['auth/login']);
      return false;
  }
}
