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

class TodoItem extends StatelessWidget {
  final Todo todo;

  const TodoItem({super.key, required this.todo});

  @override
  Widget build(BuildContext context) {
    final colorScheme = Theme.of(context).colorScheme;

    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
      child: ListTile(
        leading: Checkbox(
          value: todo.completed,
          onChanged: (_) {
            context.read<TodoState>().toggleTodo(todo.id!);
          },
        ),
        title: Text(
          todo.title,
          style: TextStyle(
            decoration:
                todo.completed ? TextDecoration.lineThrough : TextDecoration.none,
            color: todo.completed ? colorScheme.onSurfaceVariant : null,
          ),
        ),
        subtitle: Row(
          children: [
            Chip(
              label: Text(todo.category, style: const TextStyle(fontSize: 12)),
              visualDensity: VisualDensity.compact,
              padding: EdgeInsets.zero,
            ),
            const SizedBox(width: 8),
            PriorityBadge(priority: todo.priority),
          ],
        ),
        onTap: () => showTodoDialog(context, todo),
      ),
    );
  }
}
