import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:kasy_kit/components/components.dart';
import 'package:kasy_kit/core/data/models/subscription.dart';
import 'package:kasy_kit/core/theme/theme.dart';
import 'package:kasy_kit/features/subscription/ui/component/premium_page_factory.dart';
import 'package:kasy_kit/features/subscription/ui/widgets/paywall_empty_state.dart';
import 'package:kasy_kit/features/subscription/ui/widgets/premium_background_gradient.dart';
import 'package:kasy_kit/features/subscription/ui/widgets/premium_bottom_menu.dart';
import 'package:kasy_kit/features/subscription/ui/widgets/premium_close_button.dart';
import 'package:kasy_kit/features/subscription/ui/widgets/premium_feature.dart';
import 'package:kasy_kit/features/subscription/ui/widgets/selectable_row.dart';
import 'package:kasy_kit/i18n/translations.g.dart';

/// Minimal paywall: benefits list + single CTA.
/// Best for apps with one plan or when you want a clean, focused layout.
class PaywallMinimal extends StatelessWidget {
  final List<SubscriptionProduct> offers;
  final SubscriptionProduct? selectedOffer;
  final OnSelectItem<SubscriptionProduct> onSelectItem;
  final OnTapSubscription? onTap;
  final OnTap? onTapRestore;
  final OnTap? onSkip;

  const PaywallMinimal({
    super.key,
    required this.offers,
    required this.onSelectItem,
    required this.onTap,
    required this.onTapRestore,
    required this.selectedOffer,
    required this.onSkip,
  });

  // Safe because offers.isEmpty is already guarded above.
  SubscriptionProduct get _effectiveOffer => selectedOffer ?? offers.first;

  @override
  Widget build(BuildContext context) {
    if (offers.isEmpty) {
      return PaywallEmptyState(onTapRestore: onTapRestore, onSkip: onSkip);
    }

    final translations = Translations.of(context).premium;
    final features = [
      translations.feature_1,
      translations.feature_2,
      translations.feature_3,
    ];

    return Scaffold(
      backgroundColor: context.colors.primary,
      body: PremiumBackgroundGradient(
        child: SafeArea(
          child: Padding(
            padding: const EdgeInsets.symmetric(
              horizontal: KasySpacing.pageHorizontalGutter,
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Align(
                  alignment: Alignment.topRight,
                  child: AppCloseButton(onTap: () => onSkip?.call()),
                ),
                const SizedBox(height: KasySpacing.lg),
                Animate(
                  effects: const [
                    FadeEffect(
                      delay: Duration(milliseconds: 100),
                      duration: Duration(milliseconds: 300),
                    ),
                  ],
                  child: Text(
                    translations.title_1,
                    textAlign: TextAlign.center,
                    style: GoogleFonts.albertSans(
                      fontSize: 28,
                      fontWeight: FontWeight.w700,
                      color: context.colors.onPrimary,
                      letterSpacing: -0.8,
                    ),
                  ),
                ),
                const SizedBox(height: KasySpacing.lg),
                ...AnimateList(
                  interval: 120.ms,
                  delay: 300.ms,
                  effects: [FadeEffect(duration: 300.ms)],
                  children: features
                      .map((text) => PremiumFeature(text: text))
                      .toList(),
                ),
                const Spacer(),
                Animate(
                  effects: const [
                    FadeEffect(
                      delay: Duration(milliseconds: 400),
                      duration: Duration(milliseconds: 300),
                    ),
                    ScaleEffect(
                      delay: Duration(milliseconds: 400),
                      duration: Duration(milliseconds: 500),
                      curve: Curves.easeOut,
                    ),
                  ],
                  child: Text(
                    _effectiveOffer.formattedPrice(context),
                    textAlign: TextAlign.center,
                    style: context.textTheme.titleLarge?.copyWith(
                      fontWeight: FontWeight.w600,
                      color: context.colors.onPrimary,
                    ),
                  ),
                ),
                const SizedBox(height: KasySpacing.smd),
                Animate(
                  effects: const [
                    FadeEffect(
                      delay: Duration(milliseconds: 500),
                      duration: Duration(milliseconds: 300),
                    ),
                  ],
                  child: KasyButton(
                    label: translations.action_button,
                    variant: KasyButtonVariant.inverse,
                    isLoading: onTap == null,
                    expand: true,
                    onPressed: onTap,
                  ),
                ),
                const SizedBox(height: KasySpacing.md),
                BottomPremiumMenu(
                  textColor: context.colors.onPrimary,
                  onTapRestore: onTapRestore,
                ),
                const SizedBox(height: KasySpacing.lg),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
