import {
  ActivatedRouteSnapshot,
  CanActivateFn,
  Router,
  RouterStateSnapshot,
} from "@angular/router";
import { AuthService } from "../services/auth.service";
import { inject } from "@angular/core";

export const HasRoleGuard: CanActivateFn = (
  route: ActivatedRouteSnapshot,
  state: RouterStateSnapshot
) => {
  const auth = inject(AuthService);
  const router = inject(Router);

  const roleUser = auth.getLoggedUser().role.code;
  const userAccess = route.data["access_role"];
  if (userAccess.includes(roleUser)) {
    return true;
  } else {
    router.navigate(["**"]);
    return false;
  }
};
