import 'package:flutter/material.dart';
import 'package:p_tits_pas/models/parent_child_summary.dart';
import 'package:p_tits_pas/widgets/admin/common/admin_enfant_user_card.dart';

/// Liste scrollable d'enfants rattachés (fiche parent / fiche AM).
class AdminChildrenAffiliationPanel extends StatelessWidget {
  final List<ParentChildSummary> children;
  final ScrollController scrollController;
  final void Function(ParentChildSummary child) onOpen;
  final void Function(ParentChildSummary child) onDetach;
  final String emptyMessage;
  final double? height;

  static const double _itemHeight = 58;
  static const double defaultViewportHeight = _itemHeight * 2.5 + 8;

  const AdminChildrenAffiliationPanel({
    super.key,
    required this.children,
    required this.scrollController,
    required this.onOpen,
    required this.onDetach,
    this.emptyMessage = 'Aucun enfant rattaché',
    this.height,
  });

  @override
  Widget build(BuildContext context) {
    if (height != null) {
      return _panel(height!);
    }
    return LayoutBuilder(
      builder: (context, constraints) {
        final h = constraints.maxHeight.isFinite && constraints.maxHeight > 0
            ? constraints.maxHeight
            : defaultViewportHeight;
        return _panel(h);
      },
    );
  }

  Widget _panel(double panelHeight) {
    return Container(
      height: panelHeight,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        border: Border.all(color: Colors.grey.shade300),
      ),
      clipBehavior: Clip.antiAlias,
      child: children.isEmpty
          ? Center(
              child: Text(
                emptyMessage,
                style: const TextStyle(fontSize: 14, color: Colors.black54),
              ),
            )
          : ListView.builder(
              controller: scrollController,
              padding: const EdgeInsets.fromLTRB(8, 8, 8, 4),
              itemExtent: _itemHeight,
              itemCount: children.length,
              itemBuilder: (_, i) {
                final c = children[i];
                return AdminEnfantUserCard.fromSummary(
                  c,
                  onCardTap: () => onOpen(c),
                  actions: [
                    IconButton(
                      icon: const Icon(Icons.visibility_outlined),
                      tooltip: 'Voir / modifier',
                      onPressed: () => onOpen(c),
                    ),
                    IconButton(
                      tooltip: 'Détacher',
                      icon: Icon(Icons.link_off, color: Colors.orange.shade800),
                      onPressed: () => onDetach(c),
                    ),
                  ],
                );
              },
            ),
    );
  }
}
