/// Dialog pour rejeter une adhésion (saisie du motif)
library rejet_adhesion_dialog;

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../../shared/design_system/unionflow_design_system.dart';
import '../../bloc/adhesions_bloc.dart';

class RejetAdhesionDialog extends StatefulWidget {
  final String adhesionId;
  final VoidCallback onRejected;

  const RejetAdhesionDialog({
    super.key,
    required this.adhesionId,
    required this.onRejected,
  });

  @override
  State<RejetAdhesionDialog> createState() => _RejetAdhesionDialogState();
}

class _RejetAdhesionDialogState extends State<RejetAdhesionDialog> {
  final _controller = TextEditingController();
  bool _loading = false;
  bool _rejectSent = false;

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

  void _submit() {
    final motif = _controller.text.trim();
    if (motif.isEmpty) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Veuillez saisir un motif de rejet')),
      );
      return;
    }
    setState(() {
      _loading = true;
      _rejectSent = true;
    });
    context.read<AdhesionsBloc>().add(RejeterAdhesion(widget.adhesionId, motif));
  }

  @override
  Widget build(BuildContext context) {
    return BlocListener<AdhesionsBloc, AdhesionsState>(
      listenWhen: (_, state) => _rejectSent && (state.status == AdhesionsStatus.loaded || state.status == AdhesionsStatus.error),
      listener: (context, state) {
        if (!_rejectSent || !mounted) return;
        if (state.status == AdhesionsStatus.error) {
          setState(() {
            _loading = false;
            _rejectSent = false;
          });
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text(state.message ?? 'Erreur lors du rejet')),
          );
          return;
        }
        if (state.status == AdhesionsStatus.loaded) {
          setState(() => _rejectSent = false);
          widget.onRejected();
          Navigator.of(context).pop();
        }
      },
      child: AlertDialog(
      title: const Text('Rejeter la demande'),
      content: TextField(
        controller: _controller,
        decoration: const InputDecoration(
          labelText: 'Motif du rejet',
          hintText: 'Saisir le motif...',
          border: OutlineInputBorder(),
        ),
        maxLines: 3,
        enabled: !_loading,
      ),
      actions: [
        TextButton(
          onPressed: _loading ? null : () => Navigator.of(context).pop(),
          child: const Text('Annuler'),
        ),
        FilledButton(
          onPressed: _loading ? null : _submit,
          style: FilledButton.styleFrom(backgroundColor: AppColors.error),
          child: _loading
              ? const SizedBox(
                  width: 20,
                  height: 20,
                  child: CircularProgressIndicator(strokeWidth: 2),
                )
              : const Text('Rejeter'),
        ),
      ],
    ),
    );
  }
}
