import 'package:go_router/go_router.dart';

import 'package:household_keeper/features/home/presentation/home_screen.dart';
import 'package:household_keeper/features/rooms/presentation/room_form_screen.dart';
import 'package:household_keeper/features/rooms/presentation/rooms_screen.dart';
import 'package:household_keeper/features/settings/presentation/settings_screen.dart';
import 'package:household_keeper/features/tasks/presentation/task_form_screen.dart';
import 'package:household_keeper/features/tasks/presentation/task_list_screen.dart';
import 'package:household_keeper/shell/app_shell.dart';

final router = GoRouter(
  initialLocation: '/',
  routes: [
    StatefulShellRoute.indexedStack(
      builder: (context, state, navigationShell) =>
          AppShell(navigationShell: navigationShell),
      branches: [
        StatefulShellBranch(
          routes: [
            GoRoute(
              path: '/',
              builder: (context, state) => const HomeScreen(),
            ),
          ],
        ),
        StatefulShellBranch(
          routes: [
            GoRoute(
              path: '/rooms',
              builder: (context, state) => const RoomsScreen(),
              routes: [
                GoRoute(
                  path: 'new',
                  builder: (context, state) => const RoomFormScreen(),
                ),
                GoRoute(
                  path: ':roomId',
                  builder: (context, state) {
                    final roomId =
                        int.parse(state.pathParameters['roomId']!);
                    return TaskListScreen(roomId: roomId);
                  },
                  routes: [
                    GoRoute(
                      path: 'edit',
                      builder: (context, state) {
                        final roomId =
                            int.parse(state.pathParameters['roomId']!);
                        return RoomFormScreen(roomId: roomId);
                      },
                    ),
                    GoRoute(
                      path: 'tasks/new',
                      builder: (context, state) {
                        final roomId =
                            int.parse(state.pathParameters['roomId']!);
                        return TaskFormScreen(roomId: roomId);
                      },
                    ),
                    GoRoute(
                      path: 'tasks/:taskId',
                      builder: (context, state) {
                        final taskId =
                            int.parse(state.pathParameters['taskId']!);
                        return TaskFormScreen(taskId: taskId);
                      },
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
        StatefulShellBranch(
          routes: [
            GoRoute(
              path: '/settings',
              builder: (context, state) => const SettingsScreen(),
            ),
          ],
        ),
      ],
    ),
  ],
);
