import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

Page slideInTransition({required GoRouterState state, required Widget child, bool withEnterAnimation = true, bool withExitAnimation = true}) {
  return CustomTransitionPage(
    key: state.pageKey,
    child: child,
    transitionsBuilder: (context, enterAnimation, exitAnimation, child) {
      final curvedEnterAnimation = CurvedAnimation(parent: enterAnimation, curve: Curves.easeInOutCubic);
      final curvedExitAnimation = CurvedAnimation(parent: exitAnimation, curve: Curves.easeInOutCubic);

      Widget result = child;

      // Exit animation (when a new page is pushed on top of this one)
      if (withExitAnimation) {
        result = SlideTransition(
          position: Tween<Offset>(
            begin: Offset.zero,
            end: const Offset(-1.0, 0.0),
          ).animate(curvedExitAnimation),
          child: result,
        );
      }

      // Enter animation (when this page enters or when going back from this page)
      if (withEnterAnimation) {
        result = SlideTransition(
          position: Tween<Offset>(
            begin: const Offset(1.0, 0.0),
            end: Offset.zero,
          ).animate(curvedEnterAnimation),
          child: result,
        );
      } else {
        // Only animate when going back (reverse), not when entering (forward)
        result = AnimatedBuilder(
          animation: enterAnimation,
          builder: (context, animatedChild) {
            if (enterAnimation.status == AnimationStatus.reverse) {
              final offset = Tween<Offset>(
                begin: const Offset(1.0, 0.0),
                end: Offset.zero,
              ).evaluate(curvedEnterAnimation);
              return FractionalTranslation(
                translation: offset,
                child: animatedChild,
              );
            }
            return animatedChild!;
          },
          child: result,
        );
      }

      return result;
    },
  );
}