import 'package:p_tits_pas/models/parent_child_summary.dart';
import 'package:p_tits_pas/models/user.dart';

class AssistanteMaternelleModel {
  final AppUser user;
  final String? approvalNumber;
  final String? nir;
  final String? residenceCity;
  final int? maxChildren;
  final int? placesAvailable;
  final String? biography;
  final bool? available;
  final String? agreementDate;
  final List<ParentChildSummary> children;

  AssistanteMaternelleModel({
    required this.user,
    this.approvalNumber,
    this.nir,
    this.residenceCity,
    this.maxChildren,
    this.placesAvailable,
    this.biography,
    this.available,
    this.agreementDate,
    this.children = const [],
  });

  factory AssistanteMaternelleModel.fromJson(Map<String, dynamic> json) {
    final root = Map<String, dynamic>.from(json);
    final userJson = Map<String, dynamic>.from(root['user'] ?? root);
    if (root['numero_dossier'] != null && userJson['numero_dossier'] == null) {
      userJson['numero_dossier'] = root['numero_dossier'];
    }
    final user = AppUser.fromJson(userJson);
    final children = _parseChildren(root);

    return AssistanteMaternelleModel(
      user: user,
      approvalNumber: _str(
        root['approval_number'] ?? root['numero_agrement'],
      ),
      nir: _str(root['nir'] ?? root['nir_chiffre']),
      residenceCity: _str(
        root['residence_city'] ?? root['ville_residence'],
      ),
      maxChildren: _int(root['max_children'] ?? root['nb_max_enfants']),
      placesAvailable: _int(
        root['places_available'] ?? root['place_disponible'],
      ),
      biography: _str(root['biography'] ?? root['biographie']),
      available: root['available'] as bool? ?? root['disponible'] as bool?,
      agreementDate: _dateString(
        root['agreement_date'] ?? root['date_agrement'],
      ),
      children: children,
    );
  }

  static String? _str(dynamic v) {
    if (v == null) return null;
    final s = v.toString().trim();
    return s.isEmpty ? null : s;
  }

  static int? _int(dynamic v) {
    if (v == null) return null;
    if (v is int) return v;
    return int.tryParse(v.toString());
  }

  static String? _dateString(dynamic v) {
    if (v == null) return null;
    return v.toString().split('T').first;
  }

  static List<ParentChildSummary> _parseChildren(Map<String, dynamic> json) {
    final children = <ParentChildSummary>[];
    final seen = <String>{};

    void add(ParentChildSummary? child) {
      if (child == null || child.id.isEmpty || seen.contains(child.id)) return;
      seen.add(child.id);
      children.add(child);
    }

    final links = json['amChildren'] ??
        json['am_children'] ??
        json['assistanteChildren'] ??
        json['assistante_children'];
    if (links is List) {
      for (final link in links) {
        if (link is! Map) continue;
        add(ParentChildSummary.fromParentChildLink(
          Map<String, dynamic>.from(link),
        ));
      }
    }

    return children;
  }
}
