import 'package:flutter/material.dart';
import 'package:youmazgestion/Models/client.dart';

// Classe supplémentaire
class CommandeActions extends StatelessWidget {
  final Commande commande;
  final Function(int, StatutCommande) onStatutChanged;
  final Function(Commande) onGenerateBonLivraison;

  const CommandeActions({
    required this.commande,
    required this.onStatutChanged,
    required this.onGenerateBonLivraison,
  });

  List<Widget> _buildActionButtons(BuildContext context) {
    List<Widget> buttons = [];

    switch (commande.statut) {
      case StatutCommande.enAttente:
        buttons.addAll([
          // Bouton confirmer
          _buildActionButton(
            label: 'Confirmer',
            icon: Icons.check_circle,
            color: Colors.blue,
            onPressed: () => _showConfirmDialog(
              context,
              'Confirmer la commande',
              'Êtes-vous sûr de vouloir confirmer cette commande ?',
              () {
                // Change le statut à "confirmée"
                onStatutChanged(commande.id!, StatutCommande.confirmee);
                // Et génère le bon de livraison après confirmation
                onGenerateBonLivraison(commande);
              },
            ),
          ),

          // Bouton annuler
          _buildActionButton(
            label: 'Annuler',
            icon: Icons.cancel,
            color: Colors.red,
            onPressed: () => _showConfirmDialog(
              context,
              'Annuler la commande',
              'Êtes-vous sûr de vouloir annuler cette commande ?',
              () => onStatutChanged(commande.id!, StatutCommande.annulee),
            ),
          ),
        ]);
        break;

      case StatutCommande.confirmee:
        buttons.add(
          Container(
            padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
            decoration: BoxDecoration(
              color: Colors.green.shade100,
              borderRadius: BorderRadius.circular(8),
              border: Border.all(color: Colors.green.shade300),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Icon(Icons.check_circle,
                    color: Colors.green.shade600, size: 16),
                const SizedBox(width: 8),
                Text(
                  'Commande confirmée',
                  style: TextStyle(
                    color: Colors.green.shade700,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ],
            ),
          ),
        );
        break;

      case StatutCommande.annulee:
        buttons.add(
          Container(
            padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
            decoration: BoxDecoration(
              color: Colors.red.shade100,
              borderRadius: BorderRadius.circular(8),
              border: Border.all(color: Colors.red.shade300),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Icon(Icons.cancel, color: Colors.red.shade600, size: 16),
                const SizedBox(width: 8),
                Text(
                  'Commande annulée',
                  style: TextStyle(
                    color: Colors.red.shade700,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ],
            ),
          ),
        );
        break;
    }

    return buttons;
  }

  Widget _buildActionButton({
    required String label,
    required IconData icon,
    required Color color,
    required VoidCallback onPressed,
  }) {
    return ElevatedButton.icon(
      onPressed: onPressed,
      icon: Icon(icon, size: 16),
      label: Text(label),
      style: ElevatedButton.styleFrom(
        backgroundColor: color,
        foregroundColor: Colors.white,
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8),
        ),
        elevation: 2,
      ),
    );
  }

  void _showConfirmDialog(
    BuildContext context,
    String title,
    String content,
    VoidCallback onConfirm,
  ) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(12),
          ),
          title: Row(
            children: [
              Icon(
                Icons.help_outline,
                color: Colors.blue.shade600,
              ),
              const SizedBox(width: 8),
              Text(
                title,
                style: const TextStyle(fontSize: 18),
              ),
            ],
          ),
          content: Text(content),
          actions: [
            TextButton(
              onPressed: () => Navigator.of(context).pop(),
              child: Text(
                'Annuler',
                style: TextStyle(color: Colors.grey.shade600),
              ),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).pop();
                onConfirm();
              },
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.blue.shade600,
                foregroundColor: Colors.white,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
              ),
              child: const Text('Confirmer'),
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: Colors.grey.shade50,
        borderRadius: BorderRadius.circular(8),
        border: Border.all(color: Colors.grey.shade200),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          const Text(
            'Actions sur la commande',
            style: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 16,
            ),
          ),
          const SizedBox(height: 12),
          Wrap(
            spacing: 8,
            runSpacing: 8,
            children: _buildActionButtons(context),
          ),
        ],
      ),
    );
  }
}
