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

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

  user!: User;

  constructor(
    private router: Router,
    private authService: AuthService
  ) {}
  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {


      const currentUser = this.authService.isLoggedIn;
      const userVerified = this.authService.isVerified;

        if (currentUser) {
          // authorised so return true
          return true
          if (userVerified == true) {
            this.router.navigate(['/dashboard']);
          }
        }
      // not logged in so redirect to login page with the return url
      this.router.navigate(['/landing']);
      return false;
  }
}
