import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:softplayer_web/core/tokens/application/tokens_application.dart';
import 'package:softplayer_web/features/authorization/application/authorization_application.dart';
import 'package:softplayer_web/features/authorization/presentation/authorization_page.dart';
import 'package:softplayer_web/features/dashboard/presentation/dashboard_page.dart';

final goRouterProvider = Provider<GoRouter>((ref) {
  final authState = ref.watch(authorizationControllerProvider);
  final _tokenCtrl = ref.watch(tokensControllerProvider);

  // If not authorized, redirect to the auth page, otherwise dashboard
  return GoRouter(
    initialLocation: '/',
    redirect: (context, state) {
      final isAuthorized = authState.requireValue.isAuthorized;
      if (!isAuthorized) {
        return "/auth";
      }
      return "/";
    },
    routes: [
      GoRoute(path: '/', builder: (context, state) => const DashboardPage()),
      GoRoute(
        path: '/auth',
        builder: (context, state) => const AuthorizationPage(),
      ),
    ],
  );
});
