import 'package:auto_route/auto_route.dart';
import 'package:krow/core/application/routing/routes.gr.dart';
import 'package:krow/core/sevices/auth_state_service/auth_service.dart';

class SplashRedirectGuard extends AutoRouteGuard {
  final AuthService authService;

  SplashRedirectGuard(this.authService);

  @override
  Future<void> onNavigation(
    NavigationResolver resolver,
    StackRouter router,
  ) async {
    final status = await authService.getAuthStatus();

    switch (status) {
      case AuthStatus.authenticated:
        router.replace(const HomeRoute());
        break;
      case AuthStatus.adminValidation:
        router.replace(const WaitingValidationRoute());
        break;
      case AuthStatus.prepareProfile:
        //TODO(Heorhii): Some additional adjustment from backend are required before enabling this screen
        router.replace(const CheckListFlowRoute());
        break;
      case AuthStatus.unauthenticated:
        router.replace(const AuthFlowRoute());
        break;
      case AuthStatus.error:
        //todo(Artem):show error screen
        router.replace(const AuthFlowRoute());
    }

    resolver.next(false);
  }
}

class SingUpRedirectGuard extends AutoRouteGuard {
  final AuthService authService;

  SingUpRedirectGuard(this.authService);

  @override
  Future<void> onNavigation(
      NavigationResolver resolver, StackRouter router) async {
    final status = await authService.getAuthStatus();

    switch (status) {
      case AuthStatus.authenticated:
        router.replace(const HomeRoute());
        break;
      case AuthStatus.adminValidation:
        router.replace(const WaitingValidationRoute());
        break;
      case AuthStatus.prepareProfile:
        resolver.next(true);
        return;
      case AuthStatus.unauthenticated:
        router.replace(const AuthFlowRoute());
        break;
      case AuthStatus.error:
        //todo(Artem):show error screen
        router.replace(const AuthFlowRoute());
    }

    resolver.next(false);
  }
}
