import 'package:comwell_key_app/common/components/round_icon_button.dart';
import 'package:comwell_key_app/common/const.dart';
import 'package:comwell_key_app/presentation/screens/profile/profile_route.dart';
import 'package:comwell_key_app/routing/app_routes.dart';
import 'package:comwell_key_app/themes/light_theme.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

class ComwellAppBar extends StatelessWidget implements PreferredSizeWidget {
  final bool shouldShowAppBar;
  final bool shouldShowBackButton;
  final bool shouldShowProfileButton;
  final VoidCallback? onBackPressed;

  const ComwellAppBar({
    super.key,
    this.shouldShowAppBar = true,
    this.shouldShowBackButton = true,
    this.shouldShowProfileButton = true,
    this.onBackPressed,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: sandColor[20],
        borderRadius: const BorderRadius.only(
          bottomLeft: Radius.circular(24),
          bottomRight: Radius.circular(24),
        ),
      ),
      height: preferredSize.height,
      child: Padding(
        padding: const EdgeInsets.only(bottom: 16, left: 8, right: 8),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Visibility(
              visible: shouldShowBackButton,
              child: RoundIconButton(
                color: Colors.white,
                icon: "assets/icons/arrow-right.svg",
                onPressed: () {
                  if (onBackPressed != null) {
                    onBackPressed!.call();
                  } else {
                    try {
                      if (context.mounted) {
                        final goRouter = GoRouter.of(context);
                        if (goRouter.canPop()) {
                          context.pop();
                        } else {
                          context.go(AppRoutes.overview);
                        }
                      }
                    } catch (e) {
                      // Fallback to navigation to overview if pop fails
                      if (context.mounted) {
                        context.go(AppRoutes.overview);
                      }
                    }
                  }
                },
              ),
            ),
            Visibility(
              visible: shouldShowProfileButton,
              child: RoundIconButton(
                color: Colors.white,
                icon: "assets/icons/user-open.svg",
                onPressed: () {
                  ProfileRoute().push(context);
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(kComwellAppBarHeight);
}