import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';

class AuthMiddleware extends GetMiddleware {
  final tokenService = Get.find<TokenStorageService>();
  final authRouteResolver = Get.find<AuthRouteResolver>();

  @override
  RouteSettings? redirect(String? route) {
    final refreshToken = tokenService.refreshToken.value;
    final accessToken = tokenService.accessToken.value;
    final module = tokenService.appModule.value;

    if (refreshToken == null || accessToken == null) {
      if (module != null) {
        final authRoute = authRouteResolver.getAuthRouteForModule(module);
        return RouteSettings(name: authRoute, arguments: module);
      }
      return RouteSettings(name: authRouteResolver.getFallbackRoute());
    }
    return super.redirect(route);
  }
}
