
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';


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) {
         // Phase D5: Exclude set products from activity statistics (sets contain multiple items)
         final individualSakes = sakes.where((s) => s.itemType == ItemType.sake).toList();

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

         // Recording Days
         final dates = individualSakes.map((s) {
            final d = s.metadata.createdAt;
            return DateTime(d.year, d.month, d.day);
         }).toSet();
         final recordingDays = dates.length;
         
         return Container(
           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: Column(
             crossAxisAlignment: CrossAxisAlignment.start,
             children: [
               Text(
                  'あなたの活動深度',
                  style: Theme.of(context).textTheme.titleSmall?.copyWith(
                    fontWeight: FontWeight.bold,
                  ),
               ),
               const SizedBox(height: 16),
               Row(
                 mainAxisAlignment: MainAxisAlignment.spaceAround,
                 children: [
                   _buildStatItem(context, '総登録数', '$totalSakes本', LucideIcons.wine),
                   _buildStatItem(context, 'お気に入り', '$favoriteCount本', LucideIcons.heart),
                   _buildStatItem(context, '撮影日数', '$recordingDays日', LucideIcons.calendar),
                   // _buildStatItem(context, '平均価格', '¥$avgPrice', LucideIcons.banknote), // Hidden per user request
                 ],
               ),
             ],
           ),
         );
      },
      loading: () => const SizedBox.shrink(),
      error: (_, _) => const SizedBox.shrink(),
    );
  }
  
  Widget _buildStatItem(BuildContext context, String label, String value, IconData icon) {
    return Column(
      children: [
        Icon(icon, size: 20, color: Colors.grey[400]),
        const SizedBox(height: 8),
        Text(
          value,
          style: TextStyle(
            fontSize: 14,
            fontWeight: FontWeight.w900,
            color: Theme.of(context).colorScheme.onSurface,
          ),
        ),
        const SizedBox(height: 4),
        Text(
          label,
          style: TextStyle(fontSize: 10, color: Colors.grey[600]),
        ),
      ],
    );
  }
}
