import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get_navigation/src/snackbar/snackbar.dart';
import 'package:youmazgestion/Models/Remise.dart';

class DiscountDialog extends StatefulWidget {
  final Function(Remise) onDiscountApplied;

  const DiscountDialog({super.key, required this.onDiscountApplied});

  @override
  _DiscountDialogState createState() => _DiscountDialogState();
}

class _DiscountDialogState extends State<DiscountDialog> {
  RemiseType _selectedType = RemiseType.pourcentage;
  final _valueController = TextEditingController();
  final _descriptionController = TextEditingController();

  @override
  void dispose() {
    _valueController.dispose();
    _descriptionController.dispose();
    super.dispose();
  }

  void _applyDiscount() {
    final value = double.tryParse(_valueController.text) ?? 0;
    
    if (value <= 0) {
      Get.snackbar(
        'Erreur',
        'Veuillez entrer une valeur valide',
        snackPosition: SnackPosition.BOTTOM,
        backgroundColor: Colors.red,
        colorText: Colors.white,
      );
      return;
    }

    if (_selectedType == RemiseType.pourcentage && value > 100) {
      Get.snackbar(
        'Erreur',
        'Le pourcentage ne peut pas dépasser 100%',
        snackPosition: SnackPosition.BOTTOM,
        backgroundColor: Colors.red,
        colorText: Colors.white,
      );
      return;
    }

    final remise = Remise(
      type: _selectedType,
      valeur: value,
      description: _descriptionController.text,
    );

    widget.onDiscountApplied(remise);
    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
      title: Row(
        children: [
          Icon(Icons.local_offer, color: Colors.orange.shade600),
          const SizedBox(width: 8),
          const Text('Appliquer une remise'),
        ],
      ),
      content: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text('Type de remise:', style: TextStyle(fontWeight: FontWeight.w500)),
            const SizedBox(height: 8),
            Row(
              children: [
                Expanded(
                  child: RadioListTile<RemiseType>(
                    contentPadding: EdgeInsets.zero,
                    title: const Text('Pourcentage'),
                    value: RemiseType.pourcentage,
                    groupValue: _selectedType,
                    onChanged: (value) => setState(() => _selectedType = value!),
                  ),
                ),
                Expanded(
                  child: RadioListTile<RemiseType>(
                    contentPadding: EdgeInsets.zero,
                    title: const Text('Montant fixe'),
                    value: RemiseType.fixe,
                    groupValue: _selectedType,
                    onChanged: (value) => setState(() => _selectedType = value!),
                  ),
                ),
              ],
            ),
            const SizedBox(height: 16),
            
            TextField(
              controller: _valueController,
              keyboardType: TextInputType.numberWithOptions(decimal: true),
              decoration: InputDecoration(
                labelText: _selectedType == RemiseType.pourcentage 
                    ? 'Pourcentage (%)' 
                    : 'Montant (MGA)',
                prefixIcon: Icon(
                  _selectedType == RemiseType.pourcentage 
                      ? Icons.percent 
                      : Icons.attach_money,
                ),
                border: const OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 16),
            
            TextField(
              controller: _descriptionController,
              decoration: const InputDecoration(
                labelText: 'Motif de la remise (optionnel)',
                prefixIcon: Icon(Icons.note),
                border: OutlineInputBorder(),
              ),
              maxLines: 2,
            ),
            const SizedBox(height: 16),
            
            // Aperçu de la remise
            if (_valueController.text.isNotEmpty)
              Container(
                padding: const EdgeInsets.all(12),
                decoration: BoxDecoration(
                  color: Colors.orange.shade50,
                  borderRadius: BorderRadius.circular(8),
                  border: Border.all(color: Colors.orange.shade200),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const Text('Aperçu:', style: TextStyle(fontWeight: FontWeight.bold)),
                    const SizedBox(height: 4),
                    Text(
                      _selectedType == RemiseType.pourcentage
                          ? 'Remise de ${_valueController.text}%'
                          : 'Remise de ${_valueController.text} MGA',
                    ),
                    if (_descriptionController.text.isNotEmpty)
                      Text('Motif: ${_descriptionController.text}'),
                  ],
                ),
              ),
          ],
        ),
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.pop(context),
          child: const Text('Annuler'),
        ),
        ElevatedButton(
          onPressed: _applyDiscount,
          style: ElevatedButton.styleFrom(
            backgroundColor: Colors.orange.shade600,
            foregroundColor: Colors.white,
          ),
          child: const Text('Appliquer'),
        ),
      ],
    );
  }
}