import 'package:flutter/material.dart';
import 'package:flux/features/tickets/models/ticket_model.dart';

Future<({TicketStatus status, String reason})?> showPauseTicketDialog(
  BuildContext context,
) async {
  return showDialog<({TicketStatus status, String reason})>(
    context: context,
    builder: (context) => const PauseTicketDialog(),
  );
}

class PauseTicketDialog extends StatefulWidget {
  const PauseTicketDialog({super.key});

  @override
  State<PauseTicketDialog> createState() => _PauseTicketDialogState();
}

class _PauseTicketDialogState extends State<PauseTicketDialog> {
  final _reasonCtrl = TextEditingController();
  // Partiamo con un default sensato
  TicketStatus _selectedStatus = TicketStatus.waitingForParts;

  @override
  void dispose() {
    _reasonCtrl.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: const Row(
        children: [
          Icon(Icons.pause_circle_outline, color: Colors.orange),
          SizedBox(width: 8),
          Text('Sospendi Lavorazione'),
        ],
      ),
      content: SizedBox(
        // Un po' di larghezza per i chips
        width: double.maxFinite,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text('Seleziona il motivo della sospensione:'),
            const SizedBox(height: 16),

            // --- SCELTA DELLO STATO (CHIPS) ---
            Wrap(
              spacing: 8.0,
              runSpacing: 8.0,
              children: [
                ChoiceChip(
                  label: const Text('Attesa Ricambi'),
                  selected: _selectedStatus == TicketStatus.waitingForParts,
                  onSelected: (bool selected) {
                    if (selected) {
                      setState(
                        () => _selectedStatus = TicketStatus.waitingForParts,
                      );
                    }
                  },
                ),
                ChoiceChip(
                  label: const Text('Attesa Preventivo'),
                  selected: _selectedStatus == TicketStatus.waitingForQuote,
                  onSelected: (bool selected) {
                    if (selected) {
                      setState(
                        () => _selectedStatus = TicketStatus.waitingForQuote,
                      );
                    }
                  },
                ),
                ChoiceChip(
                  label: const Text('Attesa Cliente'),
                  selected: _selectedStatus == TicketStatus.waitingForCustomer,
                  onSelected: (bool selected) {
                    if (selected) {
                      setState(
                        () => _selectedStatus = TicketStatus.waitingForCustomer,
                      );
                    }
                  },
                ),
              ],
            ),

            const SizedBox(height: 24),
            const Text(
              'Note aggiuntive (opzionali):',
              style: TextStyle(fontSize: 12, color: Colors.grey),
            ),
            const SizedBox(height: 8),

            // --- NOTE AGGIUNTIVE ---
            TextField(
              controller: _reasonCtrl,
              maxLines: 2,
              decoration: const InputDecoration(
                hintText: 'Es: Il fornitore consegna martedì...',
                border: OutlineInputBorder(),
              ),
            ),
          ],
        ),
      ),
      actions: [
        TextButton(
          onPressed: () =>
              Navigator.of(context).pop(), // Ritorna null (Annulla)
          child: const Text('Annulla'),
        ),
        FilledButton(
          style: FilledButton.styleFrom(
            backgroundColor: Colors.orange.shade700,
          ),
          onPressed: () {
            // RITORNIAMO IL RECORD CON STATO E NOTE!
            Navigator.of(
              context,
            ).pop((status: _selectedStatus, reason: _reasonCtrl.text.trim()));
          },
          child: const Text('Conferma Pausa'),
        ),
      ],
    );
  }
}
