import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../providers/reports_provider.dart';

/// Collapsible panel for toggling which report widgets to include
/// on the screen and in the exported PDF.
class ReportWidgetSelector extends ConsumerWidget {
  const ReportWidgetSelector({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final enabled = ref.watch(reportWidgetToggleProvider);
    final theme = Theme.of(context);
    final text = theme.textTheme;

    return Card(
      child: ExpansionTile(
        leading: const Icon(Icons.widgets_outlined, size: 20),
        title: Text('Widgets to Include', style: text.titleSmall),
        subtitle: Text(
          '${enabled.length} of ${ReportWidgetType.values.length} selected',
          style: text.bodySmall,
        ),
        childrenPadding: const EdgeInsets.fromLTRB(16, 0, 16, 12),
        children: [
          // Quick actions
          Row(
            children: [
              TextButton.icon(
                onPressed: () {
                  ref.read(reportWidgetToggleProvider.notifier).state =
                      ReportWidgetType.values.toSet();
                },
                icon: const Icon(Icons.select_all, size: 16),
                label: const Text('Select All'),
              ),
              const SizedBox(width: 8),
              TextButton.icon(
                onPressed: () {
                  ref.read(reportWidgetToggleProvider.notifier).state = {};
                },
                icon: const Icon(Icons.deselect, size: 16),
                label: const Text('Deselect All'),
              ),
            ],
          ),
          const SizedBox(height: 4),
          // Group by section
          for (final section in ReportSection.values) ...[
            Padding(
              padding: const EdgeInsets.only(top: 8, bottom: 4),
              child: Text(
                section.label,
                style: text.labelMedium?.copyWith(
                  color: theme.colorScheme.primary,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ),
            Wrap(
              spacing: 8,
              runSpacing: 4,
              children: ReportWidgetType.values
                  .where((w) => w.section == section)
                  .map((w) {
                    final isSelected = enabled.contains(w);
                    return FilterChip(
                      label: Text(w.label),
                      selected: isSelected,
                      onSelected: (selected) {
                        final current = Set<ReportWidgetType>.from(enabled);
                        if (selected) {
                          current.add(w);
                        } else {
                          current.remove(w);
                        }
                        ref.read(reportWidgetToggleProvider.notifier).state =
                            current;
                      },
                    );
                  })
                  .toList(),
            ),
          ],
        ],
      ),
    );
  }
}
