import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:niogu_app/core/components/bottom_bar_app.dart';
import 'package:niogu_app/core/providers/app_provider.dart';
import 'package:niogu_app/core/enums/user_role.dart';
import 'package:niogu_app/core/widgets/pop_up_notification.dart';

class MainWrapper extends ConsumerWidget {
  final StatefulNavigationShell navigationShell;

  const MainWrapper({super.key, required this.navigationShell});

  static final GlobalKey<ScaffoldState> scaffoldKey =
      GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return LayoutBuilder(
      builder: (context, constraints) {
        final messageActivity = ref.watch(messageActivityProvider);

        final currentUserRole = ref.watch(currentUserRoleProvider);

        return SafeArea(
          top: false,
          bottom: true,
          right: false,
          left: false,
          child: Stack(
            children: [
              Scaffold(
                key: scaffoldKey,
                extendBody: true,
                resizeToAvoidBottomInset: false,
                body: navigationShell,
                bottomNavigationBar: navigationShell.currentIndex == 2
                    ? null
                    : BottomBarApp(
                        currentIndex: navigationShell.currentIndex,
                        onTap: (index) {
                          navigationShell.goBranch(
                            index,
                            initialLocation:
                                index == navigationShell.currentIndex,
                          );
                        },
                      ),
              ),
              if (messageActivity != null)
                PopupNotification(
                  isOwner: currentUserRole == UserRole.owner,
                  messages: messageActivity.messages,
                  type: messageActivity.type,
                ),
            ],
          ),
        );
      },
    );
  }
}
