import 'package:flutter/material.dart';

import 'package:household_keeper/features/tasks/domain/effort_level.dart';
import 'package:household_keeper/features/tasks/domain/frequency.dart';
import 'package:household_keeper/features/templates/data/task_templates.dart';
import 'package:household_keeper/l10n/app_localizations.dart';

/// Bottom sheet with a checklist of task templates for a detected room type.
///
/// Displays German-language task templates with frequency and effort labels.
/// All templates start unchecked. The user can select desired templates and
/// confirm, or skip (dismiss) without selecting any.
class TemplatePickerSheet extends StatefulWidget {
  const TemplatePickerSheet({
    super.key,
    required this.roomType,
    required this.templates,
    required this.onConfirm,
  });

  /// The detected room type key (e.g. "kueche").
  final String roomType;

  /// The template list for this room type.
  final List<TaskTemplate> templates;

  /// Callback invoked with the selected templates when user confirms.
  final void Function(List<TaskTemplate> selected) onConfirm;

  @override
  State<TemplatePickerSheet> createState() => _TemplatePickerSheetState();
}

class _TemplatePickerSheetState extends State<TemplatePickerSheet> {
  /// Tracks which template indices are checked.
  final Set<int> _selected = {};

  /// Display name for the detected room type (first letter capitalized).
  String get _roomTypeDisplayName {
    final key = widget.roomType;
    if (key.isEmpty) return key;
    return key[0].toUpperCase() + key.substring(1);
  }

  /// German label combining frequency and effort for a template.
  String _templateSubtitle(TaskTemplate template) {
    final freq = FrequencyInterval(
      intervalType: template.intervalType,
      days: template.intervalDays,
    ).label();
    final effort = template.effortLevel.label();
    return '$freq \u00b7 $effort';
  }

  void _onConfirmPressed() {
    final selectedTemplates = _selected
        .map((index) => widget.templates[index])
        .toList(growable: false);
    widget.onConfirm(selectedTemplates);
  }

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context);
    final theme = Theme.of(context);

    return DraggableScrollableSheet(
      initialChildSize: 0.6,
      minChildSize: 0.3,
      maxChildSize: 0.9,
      expand: false,
      builder: (context, scrollController) {
        return Column(
          children: [
            // Drag handle
            Padding(
              padding: const EdgeInsets.only(top: 12, bottom: 4),
              child: Container(
                width: 32,
                height: 4,
                decoration: BoxDecoration(
                  color: theme.colorScheme.onSurfaceVariant.withValues(alpha: 0.4),
                  borderRadius: BorderRadius.circular(2),
                ),
              ),
            ),
            // Title and subtitle
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    l10n.templatePickerTitle,
                    style: theme.textTheme.titleLarge,
                  ),
                  const SizedBox(height: 4),
                  Text(
                    _roomTypeDisplayName,
                    style: theme.textTheme.bodyMedium?.copyWith(
                      color: theme.colorScheme.onSurfaceVariant,
                    ),
                  ),
                ],
              ),
            ),
            const Divider(height: 1),
            // Template checklist
            Expanded(
              child: ListView.builder(
                controller: scrollController,
                itemCount: widget.templates.length,
                itemBuilder: (context, index) {
                  final template = widget.templates[index];
                  final isChecked = _selected.contains(index);
                  return CheckboxListTile(
                    value: isChecked,
                    onChanged: (checked) {
                      setState(() {
                        if (checked == true) {
                          _selected.add(index);
                        } else {
                          _selected.remove(index);
                        }
                      });
                    },
                    title: Text(template.name),
                    subtitle: Text(_templateSubtitle(template)),
                  );
                },
              ),
            ),
            // Selected count indicator
            if (_selected.isNotEmpty)
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 16),
                child: Text(
                  l10n.templatePickerSelected(_selected.length),
                  style: theme.textTheme.bodySmall?.copyWith(
                    color: theme.colorScheme.primary,
                  ),
                ),
              ),
            // Action buttons
            Padding(
              padding: const EdgeInsets.all(16),
              child: Row(
                children: [
                  Expanded(
                    child: OutlinedButton(
                      onPressed: () => Navigator.of(context).pop(),
                      child: Text(l10n.templatePickerSkip),
                    ),
                  ),
                  const SizedBox(width: 12),
                  Expanded(
                    child: FilledButton(
                      onPressed:
                          _selected.isNotEmpty ? _onConfirmPressed : null,
                      child: Text(l10n.templatePickerAdd),
                    ),
                  ),
                ],
              ),
            ),
          ],
        );
      },
    );
  }
}

/// Shows the template picker bottom sheet and returns the selected templates.
///
/// Returns the list of selected [TaskTemplate]s, or null if user skipped.
Future<List<TaskTemplate>?> showTemplatePickerSheet({
  required BuildContext context,
  required String roomType,
  required List<TaskTemplate> templates,
}) {
  return showModalBottomSheet<List<TaskTemplate>>(
    context: context,
    isScrollControlled: true,
    useSafeArea: true,
    shape: const RoundedRectangleBorder(
      borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
    ),
    builder: (context) {
      return TemplatePickerSheet(
        roomType: roomType,
        templates: templates,
        onConfirm: (selected) {
          Navigator.of(context).pop(selected);
        },
      );
    },
  );
}
