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

@Injectable({
  providedIn: 'root'
})
export class PasajeroGuard implements CanActivate, CanLoad {
  constructor(private authPasajeroService: AuthPasajerosService,private router: Router){}

  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
      if (this.authPasajeroService.verificarAuth() && this.authPasajeroService.getPasajeroRole() == 'pasajero') {
        return true;
      }
      this.router.navigate(['auth/login']);
      return false;
  }
  canLoad(
    route: Route,
    segments: UrlSegment[]): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
      if (this.authPasajeroService.verificarAuth() && this.authPasajeroService.getPasajeroRole() == 'pasajero') {
        return true;
      }
      this.router.navigate(['auth/login']);
      return false;
  }
}
