import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/todo.dart';
import '../state/todo_state.dart';

Future<void> showTodoDialog(BuildContext context, Todo? todo) async {
  final editing = todo != null;
  final titleCtl = TextEditingController(text: todo?.title ?? '');
  String selectedCategory = todo?.category ?? 'Personal';
  String selectedPriority = todo?.priority ?? 'medium';

  await showDialog(
    context: context,
    builder: (dialogCtx) {
      return StatefulBuilder(
        builder: (ctx, setState) {
          return AlertDialog(
            title: Text(editing ? 'Edit Todo' : 'Add New Todo'),
            content: SingleChildScrollView(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  TextField(
                    controller: titleCtl,
                    decoration: const InputDecoration(
                      labelText: 'What needs to be done?',
                      border: OutlineInputBorder(),
                    ),
                    autofocus: true,
                  ),
                  const SizedBox(height: 16),
                  const Text('Category',
                      style: TextStyle(fontWeight: FontWeight.w600)),
                  const SizedBox(height: 8),
                  Wrap(
                    spacing: 8,
                    children: categories.map((c) {
                      return ChoiceChip(
                        label: Text(c),
                        selected: c == selectedCategory,
                        onSelected: (_) {
                          setState(() => selectedCategory = c);
                        },
                      );
                    }).toList(),
                  ),
                  const SizedBox(height: 16),
                  const Text('Priority',
                      style: TextStyle(fontWeight: FontWeight.w600)),
                  const SizedBox(height: 8),
                  SegmentedButton<String>(
                    segments: priorityKeys.map((p) {
                      return ButtonSegment(value: p, label: Text(p));
                    }).toList(),
                    selected: {selectedPriority},
                    onSelectionChanged: (selection) {
                      setState(() => selectedPriority = selection.first);
                    },
                  ),
                ],
              ),
            ),
            actions: [
              TextButton(
                onPressed: () => Navigator.of(dialogCtx).pop(),
                child: const Text('Cancel'),
              ),
              FilledButton(
                onPressed: () {
                  final title = titleCtl.text.trim();
                  if (title.isEmpty) return;
                  final state = context.read<TodoState>();
                  if (editing) {
                    state.updateTodo(
                        todo!.id!, title, selectedCategory, selectedPriority);
                  } else {
                    state.addTodo(title, selectedCategory, selectedPriority);
                  }
                  Navigator.of(dialogCtx).pop();
                },
                child: Text(editing ? 'Save' : 'Add'),
              ),
            ],
          );
        },
      );
    },
  );
}
