import { Injectable } from '@angular/core';
import { CanLoad, Route, UrlSegment, Router } from '@angular/router';
import { environment } from '../../environments/environment';

type FeatureKey = keyof typeof environment.features;

@Injectable({ providedIn: 'root' })
export class FeatureFlagGuard implements CanLoad {

  constructor(private router: Router) {}

  canLoad(route: Route, _segments: UrlSegment[]): boolean {

    const key = route.data?.['featureFlag'] as FeatureKey | undefined;

    if (!key) return true;

    const enabled = environment.features[key];
    if (enabled) return true;

    this.router.navigateByUrl('error/404');
    return false;
  }
}
