import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lucide_icons/lucide_icons.dart';
import '../../providers/sake_list_provider.dart';
import '../../models/schema/item_type.dart';
import '../../theme/app_colors.dart';

class ActivityStats extends ConsumerWidget {
  const ActivityStats({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final allSakeAsync = ref.watch(allSakeItemsProvider);

    return allSakeAsync.when(
      data: (sakes) {
        final individualSakes = sakes.where((s) => s.itemType == ItemType.sake).toList();

        final totalSakes = individualSakes.length;
        final favoriteCount = individualSakes.where((s) => s.userData.isFavorite).length;

        final dates = individualSakes.map((s) {
          final d = s.metadata.createdAt;
          return DateTime(d.year, d.month, d.day);
        }).toSet();
        final recordingDays = dates.length;

        final appColors = Theme.of(context).extension<AppColors>()!;

        // Bento Grid: 総登録数を大カード、お気に入り・撮影日数を小カード2枚横並び
        return Column(
          children: [
            // 大カード — 総登録数
            _BentoCard(
              child: Row(
                children: [
                  Container(
                    padding: const EdgeInsets.all(12),
                    decoration: BoxDecoration(
                      color: appColors.brandPrimary.withValues(alpha: 0.1),
                      borderRadius: BorderRadius.circular(12),
                    ),
                    child: Icon(LucideIcons.wine, size: 28, color: appColors.brandPrimary),
                  ),
                  const SizedBox(width: 16),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '$totalSakes本',
                        style: TextStyle(
                          fontSize: 32,
                          fontWeight: FontWeight.w900,
                          color: appColors.brandPrimary,
                          height: 1.0,
                        ),
                      ),
                      const SizedBox(height: 4),
                      Text(
                        '総登録数',
                        style: TextStyle(
                          fontSize: 12,
                          color: appColors.textSecondary,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
            const SizedBox(height: 8),
            // 小カード2枚横並び
            Row(
              children: [
                Expanded(
                  child: _BentoCard(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Icon(LucideIcons.heart, size: 20, color: appColors.brandAccent),
                        const SizedBox(height: 8),
                        Text(
                          '$favoriteCount本',
                          style: TextStyle(
                            fontSize: 22,
                            fontWeight: FontWeight.w900,
                            color: appColors.textPrimary,
                            height: 1.0,
                          ),
                        ),
                        const SizedBox(height: 4),
                        Text(
                          'お気に入り',
                          style: TextStyle(fontSize: 11, color: appColors.textSecondary),
                        ),
                      ],
                    ),
                  ),
                ),
                const SizedBox(width: 8),
                Expanded(
                  child: _BentoCard(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Icon(LucideIcons.calendar, size: 20, color: appColors.iconDefault),
                        const SizedBox(height: 8),
                        Text(
                          '$recordingDays日',
                          style: TextStyle(
                            fontSize: 22,
                            fontWeight: FontWeight.w900,
                            color: appColors.textPrimary,
                            height: 1.0,
                          ),
                        ),
                        const SizedBox(height: 4),
                        Text(
                          '撮影日数',
                          style: TextStyle(fontSize: 11, color: appColors.textSecondary),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ],
        );
      },
      loading: () => const SizedBox.shrink(),
      error: (_, _) => const SizedBox.shrink(),
    );
  }
}

class _BentoCard extends StatelessWidget {
  const _BentoCard({required this.child});
  final Widget child;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Theme.of(context).cardColor,
        borderRadius: BorderRadius.circular(16),
        border: Border.all(
          color: Theme.of(context).dividerColor.withValues(alpha: 0.1),
        ),
      ),
      child: child,
    );
  }
}
