import 'package:flutter/material.dart';
import 'validation_modal_theme.dart';

/// Page « Motifs du refus » : champ libre + Annuler (ferme la modale), Précédent (retour au choix Valider/Refuser), Envoyer. Ticket #107.
class ValidationRefusForm extends StatefulWidget {
  /// Ferme la modale (abandon du flux).
  final VoidCallback onCancel;
  /// Retour à l’étape précédente du wizard (écran avec Valider / Refuser).
  final VoidCallback onPrevious;
  final ValueChanged<String?> onSubmit;
  /// Pendant l'appel API : désactive les actions (ticket #110).
  final bool isSubmitting;

  const ValidationRefusForm({
    super.key,
    required this.onCancel,
    required this.onPrevious,
    required this.onSubmit,
    this.isSubmitting = false,
  });

  @override
  State<ValidationRefusForm> createState() => _ValidationRefusFormState();
}

class _ValidationRefusFormState extends State<ValidationRefusForm> {
  final _controller = TextEditingController();
  final _formKey = GlobalKey<FormState>();

  static const int _minLength = 20;

  String? _validateMotifs(String? value) {
    final t = value?.trim() ?? '';
    if (t.isEmpty) return 'Les motifs du refus sont obligatoires.';
    if (t.length < _minLength) {
      return 'Veuillez indiquer au moins $_minLength caractères (${t.length}/$_minLength).';
    }
    return null;
  }

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

  @override
  Widget build(BuildContext context) {
    return Form(
      key: _formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Text(
            'Indiquez les motifs du refus',
            style: Theme.of(context).textTheme.titleLarge?.copyWith(
                  fontWeight: FontWeight.w600,
                ),
          ),
          const SizedBox(height: 16),
          Expanded(
            child: LayoutBuilder(
              builder: (context, constraints) {
                return Container(
                  constraints: BoxConstraints.tight(Size(constraints.maxWidth, constraints.maxHeight)),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(4),
                    border: Border.all(color: Colors.grey.shade400),
                  ),
                  child: TextFormField(
                    controller: _controller,
                    readOnly: widget.isSubmitting,
                    maxLines: null,
                    minLines: 1,
                    validator: _validateMotifs,
                    decoration: InputDecoration(
                      hintText: 'Saisissez les raisons du refus (minimum $_minLength caractères)',
                      border: InputBorder.none,
                      enabledBorder: InputBorder.none,
                      focusedBorder: InputBorder.none,
                      alignLabelWithHint: true,
                      filled: false,
                      contentPadding: const EdgeInsets.symmetric(
                        horizontal: 12,
                        vertical: 12,
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
          const SizedBox(height: 24),
          Row(
            children: [
              TextButton(
                onPressed: widget.isSubmitting ? null : widget.onCancel,
                child: const Text('Annuler'),
              ),
              const Spacer(),
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  TextButton(
                    onPressed: widget.isSubmitting ? null : widget.onPrevious,
                    child: const Text('Précédent'),
                  ),
                  const SizedBox(width: 8),
                  if (widget.isSubmitting)
                    const Padding(
                      padding: EdgeInsets.symmetric(horizontal: 12),
                      child: SizedBox(
                        width: 22,
                        height: 22,
                        child: CircularProgressIndicator(strokeWidth: 2),
                      ),
                    )
                  else
                    ElevatedButton(
                      style: ValidationModalTheme.primaryElevatedStyle,
                      onPressed: () {
                        if (_formKey.currentState!.validate()) {
                          widget.onSubmit(_controller.text.trim());
                        }
                      },
                      child: const Text('Envoyer'),
                    ),
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }
}
