import 'package:go_router/go_router.dart';

import 'package:pweb/app/router/pages.dart';
import 'package:pweb/pages/2fa/page.dart';
import 'package:pweb/pages/signup/page.dart';
import 'package:pweb/widgets/sidebar/page.dart';
import 'package:pweb/pages/login/page.dart';
import 'package:pweb/pages/errors/not_found.dart';


GoRouter createRouter() => GoRouter(
  debugLogDiagnostics: true,
  routes: <RouteBase>[
    GoRoute(
      name: Pages.root.name,
      path: routerPage(Pages.root),
      builder: (_, _) => const LoginPage(),
      routes: [
        GoRoute(
          name: Pages.login.name,
          path: routerPage(Pages.login),
          builder: (_, _) => const LoginPage(),
        ),
        GoRoute(
          name: Pages.dashboard.name,
          path: routerPage(Pages.dashboard),
          builder: (_, _) => const PageSelector(),
        ),
        GoRoute(
          name: Pages.sfactor.name,
          path: routerPage(Pages.sfactor),
          builder: (context, state) {
            // Определяем откуда пришел пользователь
            final isFromSignup = state.uri.queryParameters['from'] == 'signup';
            
            return TwoFactorCodePage(
              onVerificationSuccess: () {
                if (isFromSignup) {
                  // После регистрации -> на страницу логина
                  context.goNamed(Pages.login.name);
                } else {
                  // После логина -> на дашборд
                  context.goNamed(Pages.dashboard.name);
                }
              },
            );
          },
        ),
        GoRoute(
          name: Pages.signup.name,
          path: routerPage(Pages.signup),
          builder: (_, _) => const SignUpPage(),
        ),
      ],
    ),
  ],
  errorBuilder: (_, _) => const NotFoundPage(),
);
