import 'package:flutter/material.dart';
import 'package:kasy_kit/components/kasy_button.dart';
import 'package:kasy_kit/core/theme/theme.dart';
import 'package:kasy_kit/features/onboarding/ui/widgets/onboarding_progress.dart';
import 'package:kasy_kit/features/onboarding/ui/widgets/onboarding_sticky_footer.dart';

/// Layout compartilhado para passos de onboarding com ilustração e ações no rodapé.
///
/// Evita buracos grandes de [Expanded] + [Spacer] com imagens [BoxFit.contain].
class OnboardingIllustrationScaffold extends StatelessWidget {
  const OnboardingIllustrationScaffold({
    super.key,
    required this.progress,
    required this.title,
    required this.description,
    required this.footerActions,
    this.imageAsset,
    this.image,
    this.imageMaxHeight = 280,
    this.titleWidget,
    this.descriptionWidget,
    this.footerAnimationDelayMs = 400,
    this.animateFooter = true,
    this.onSkip,
    this.skipLabel,
  });

  final double progress;
  final String title;
  final String description;
  final String? imageAsset;
  final Widget? image;
  final List<Widget> footerActions;
  final double imageMaxHeight;
  final Widget? titleWidget;
  final Widget? descriptionWidget;
  final int footerAnimationDelayMs;
  final bool animateFooter;
  final VoidCallback? onSkip;
  final String? skipLabel;

  @override
  Widget build(BuildContext context) {
    final Widget? illustration = image ??
        (imageAsset != null && imageAsset!.isNotEmpty
            ? Image.asset(imageAsset!, fit: BoxFit.contain)
            : null);

    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        Expanded(
          child: SafeArea(
            bottom: false,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                if (onSkip != null)
                  Align(
                    alignment: Alignment.centerRight,
                    child: KasyButton(
                      label: skipLabel ?? 'Skip',
                      variant: KasyButtonVariant.ghost,
                      size: KasyButtonSize.small,
                      onPressed: onSkip,
                    ),
                  ),
                OnboardingProgress(value: progress),
                Expanded(
                  child: SingleChildScrollView(
                    padding: const EdgeInsets.fromLTRB(
                      KasySpacing.xl,
                      KasySpacing.lg,
                      KasySpacing.xl,
                      KasySpacing.md,
                    ),
                    child: Column(
                      children: [
                        titleWidget ??
                            Text(
                              title,
                              textAlign: TextAlign.center,
                              style:
                                  context.textTheme.headlineMedium?.copyWith(
                                color: context.colors.onBackground,
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                        const SizedBox(height: KasySpacing.smd),
                        descriptionWidget ??
                            Text(
                              description,
                              textAlign: TextAlign.center,
                              style: context.textTheme.bodyLarge?.copyWith(
                                color: context.colors.onBackground.withValues(
                                  alpha: 0.6,
                                ),
                              ),
                            ),
                        if (illustration != null) ...[
                          const SizedBox(height: KasySpacing.xl),
                          ConstrainedBox(
                            constraints:
                                BoxConstraints(maxHeight: imageMaxHeight),
                            child: illustration,
                          ),
                        ],
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        OnboardingStickyFooter(
          animate: animateFooter,
          animationDelayMs: footerAnimationDelayMs,
          horizontalPadding: KasySpacing.xl,
          children: footerActions,
        ),
      ],
    );
  }
}
