import 'package:flutter/material.dart';
import 'package:p_tits_pas/models/enfant_admin_model.dart';
import 'package:p_tits_pas/models/parent_child_summary.dart';
import 'package:p_tits_pas/utils/date_display_utils.dart';
import 'package:p_tits_pas/utils/enfant_status_utils.dart';
import 'package:p_tits_pas/utils/enfant_vigilance.dart';
import 'package:p_tits_pas/widgets/admin/common/admin_user_card.dart';

List<String> enfantAdminSubtitleLines({
  required String status,
  String? birthDate,
  String? dueDate,
  String? gender,
  List<String> extra = const [],
}) {
  final lines = <String>[];
  final age = formatChildAgeLabel(
    birthDate: birthDate,
    dueDate: dueDate,
    status: normalizeEnfantStatus(status),
  );
  if (age.isNotEmpty) lines.add(age);
  final normalized = normalizeEnfantStatus(status);
  if (normalized.isNotEmpty) {
    lines.add('Statut : ${enfantStatusLabel(normalized, gender: gender)}');
  }
  lines.addAll(extra);
  return lines;
}

/// Carte enfant admin (photo, nom, âge) — même rendu onglet Enfants / fiche parent.
class AdminEnfantUserCard extends StatelessWidget {
  final String title;
  final String? photoUrl;
  final List<String> subtitleLines;
  final List<Widget> actions;
  final VoidCallback? onCardTap;
  final EdgeInsetsGeometry? margin;
  final EdgeInsetsGeometry? contentPadding;
  final Color? borderColor;
  final String? vigilanceTooltip;

  const AdminEnfantUserCard({
    super.key,
    required this.title,
    this.photoUrl,
    required this.subtitleLines,
    this.actions = const [],
    this.onCardTap,
    this.margin,
    this.contentPadding,
    this.borderColor,
    this.vigilanceTooltip,
  });

  factory AdminEnfantUserCard.fromEnfant(
    EnfantAdminModel enfant, {
    List<String> extraSubtitleLines = const [],
    List<Widget> actions = const [],
    VoidCallback? onCardTap,
    EdgeInsetsGeometry? margin,
    EdgeInsetsGeometry? contentPadding,
    Color? borderColor,
    String? vigilanceTooltip,
  }) {
    final parents = enfant.parentLinks
        .map((l) => l.parentName ?? 'Parent')
        .join(', ');
    final orphan = enfantHasNoResponsable(enfant);
    return AdminEnfantUserCard(
      title: enfant.fullName,
      photoUrl: enfant.photoUrl,
      subtitleLines: enfantAdminSubtitleLines(
        status: enfant.status,
        birthDate: enfant.birthDate,
        dueDate: enfant.dueDate,
        gender: enfant.gender,
        extra: [
          if (parents.isNotEmpty) 'Responsables : $parents',
          if (orphan) 'Aucun responsable rattaché',
          ...extraSubtitleLines,
        ],
      ),
      actions: actions,
      onCardTap: onCardTap,
      margin: margin,
      contentPadding: contentPadding,
      borderColor: borderColor ??
          (orphan ? Colors.red.shade300 : null),
      vigilanceTooltip: vigilanceTooltip ??
          enfantSansResponsableVigilanceMessage(enfant),
    );
  }

  factory AdminEnfantUserCard.fromSummary(
    ParentChildSummary child, {
    List<String> extraSubtitleLines = const [],
    List<Widget> actions = const [],
    VoidCallback? onCardTap,
    EdgeInsetsGeometry? margin,
    EdgeInsetsGeometry? contentPadding,
  }) {
    return AdminEnfantUserCard(
      title: child.fullName,
      photoUrl: child.photoUrl,
      subtitleLines: enfantAdminSubtitleLines(
        status: child.status,
        birthDate: child.birthDate,
        dueDate: child.dueDate,
        extra: extraSubtitleLines,
      ),
      actions: actions,
      onCardTap: onCardTap,
      margin: margin,
      contentPadding: contentPadding,
    );
  }

  @override
  Widget build(BuildContext context) {
    return AdminUserCard(
      title: title,
      fallbackIcon: Icons.child_care_outlined,
      avatarUrl: photoUrl,
      subtitleLines: subtitleLines,
      actions: actions,
      onCardTap: onCardTap,
      margin: margin,
      contentPadding: contentPadding,
      borderColor: borderColor,
      vigilanceTooltip: vigilanceTooltip,
    );
  }
}
