import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:kasy_kit/components/kasy_app_bar.dart';
import 'package:kasy_kit/components/kasy_card.dart';
import 'package:kasy_kit/core/config/features.dart';
import 'package:kasy_kit/core/haptics/kasy_haptics.dart';
import 'package:kasy_kit/core/theme/theme.dart';
import 'package:kasy_kit/core/widgets/kasy_hover.dart';
import 'package:kasy_kit/core/widgets/kasy_scroll_behavior.dart';
import 'package:kasy_kit/features/notifications/api/local_notifier.dart';
import 'package:kasy_kit/features/notifications/providers/models/notification.dart'
    as kasy_kit;
import 'package:kasy_kit/features/settings/ui/components/admin/admin_routes.dart';
import 'package:kasy_kit/i18n/translations.g.dart';

/// Feature list shown from [HomePage] (paywall, notifications, etc.).
class HomeFeaturesPage extends ConsumerWidget {
  const HomeFeaturesPage({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final rows = _visibleFeatureRows(context, ref);
    final Color bg = context.colors.background;

    return Scaffold(
      backgroundColor: bg,
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          KasyAppBar(
            title: t.home.features_page.title,
            onBack: () => Navigator.maybePop(context),
          ),
          Expanded(
            child: ScrollConfiguration(
              behavior: const KasyKitScrollBehavior(),
              child: SingleChildScrollView(
                padding: EdgeInsets.fromLTRB(
                  KasySpacing.pageHorizontalGutter,
                  KasySpacing.belowChromeContentGap,
                  KasySpacing.pageHorizontalGutter,
                  MediaQuery.paddingOf(context).bottom +
                      KasySpacing.belowChromeContentGap,
                ),
                child: KasyCard(
                  variant: KasyCardVariant.outlined,
                  color: context.colors.surface,
                  borderRadius: KasyRadius.lgBorderRadius,
                  child: Column(
                    children: [
                      for (int i = 0; i < rows.length; i++) ...[
                        KasyHover(
                          hapticEnabled: false,
                          onTap: () {
                            KasyHaptics.selection(context);
                            rows[i].onTap();
                          },
                          child: Padding(
                            padding: const EdgeInsets.symmetric(
                              horizontal: KasySpacing.smd,
                              vertical: 14,
                            ),
                            child: Row(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Padding(
                                  padding: const EdgeInsets.only(top: 2),
                                  child: Icon(
                                    rows[i].icon,
                                    size: 20,
                                    color: context.colors.onSurface
                                        .withValues(alpha: 0.45),
                                  ),
                                ),
                                const SizedBox(width: KasySpacing.smd),
                                Expanded(
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Text(
                                        rows[i].title,
                                        style: context.textTheme.bodyLarge
                                            ?.copyWith(
                                          fontWeight: FontWeight.w600,
                                          color: context.colors.onSurface,
                                        ),
                                      ),
                                      const SizedBox(height: 3),
                                      Text(
                                        rows[i].subtitle,
                                        style: context.textTheme.bodySmall
                                            ?.copyWith(
                                          color: context.colors.muted,
                                          height: 1.35,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                Padding(
                                  padding: const EdgeInsets.only(
                                    left: KasySpacing.xs,
                                    top: 3,
                                  ),
                                  child: Icon(
                                    KasyIcons.chevronRight,
                                    color: context.colors.muted,
                                    size: 18,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                        if (i < rows.length - 1)
                          Divider(
                            height: 1,
                            color: context.colors.onBackground
                                .withValues(alpha: 0.06),
                          ),
                      ],
                    ],
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class _DemoFeatureRow {
  final IconData icon;
  final String title;
  final String subtitle;
  final VoidCallback onTap;

  const _DemoFeatureRow({
    required this.icon,
    required this.title,
    required this.subtitle,
    required this.onTap,
  });
}

List<_DemoFeatureRow> _visibleFeatureRows(BuildContext context, WidgetRef ref) {
  final page = t.home.features_page;
  return [
    if (withLlmChat)
      _DemoFeatureRow(
        icon: KasyIcons.assistant,
        title: page.assistant_title,
        subtitle: page.assistant_description,
        onTap: () => context.push('/assistant'),
      ),
    if (withFeedback)
      _DemoFeatureRow(
        icon: KasyIcons.message,
        title: page.feedback_title,
        subtitle: page.feedback_description,
        onTap: () => context.push('/feedback'),
      ),
    _DemoFeatureRow(
      icon: KasyIcons.notification,
      title: page.notification_title,
      subtitle: page.notification_description,
      onTap: () {
        final settings = ref.read(notificationsSettingsProvider);
        final localNotifier = ref.read(localNotifierProvider);
        kasy_kit.Notification.withData(
          id: 'fake-id',
          title: page.notification_demo_title,
          body: page.notification_demo_body,
          createdAt: DateTime.now(),
          notifier: localNotifier,
          notifierSettings: settings,
        ).show();
      },
    ),
    _DemoFeatureRow(
      icon: KasyIcons.notificationActive,
      title: page.send_push_title,
      subtitle: page.send_push_description,
      onTap: () => context.push(adminRouteSendPush),
    ),
    if (withRevenuecat)
      _DemoFeatureRow(
        icon: KasyIcons.payment,
        title: page.paywall_title,
        subtitle: page.paywall_description,
        onTap: () => context.push('/premium'),
      ),
  ];
}

/// Number of demo entries currently visible (matches list on this page).
int homeFeaturesEntryCount() {
  var n = 2; // notification local + send push (always present)
  if (withRevenuecat) n++;
  if (withFeedback) n++;
  if (withLlmChat) n++;
  return n;
}
