class AppUser {
  final String id;
  final String email;
  final String role;
  final DateTime createdAt;
  final DateTime updatedAt;
  final bool changementMdpObligatoire;
  final String? nom;
  final String? prenom;
  final String? statut;
  final String? telephone;
  final String? photoUrl;
  final String? adresse;
  final String? ville;
  final String? codePostal;
  final String? relaisId;
  final String? relaisNom;
  final String? numeroDossier;
  final String? dateNaissance;
  final String? lieuNaissanceVille;
  final String? lieuNaissancePays;

  AppUser({
    required this.id,
    required this.email,
    required this.role,
    required this.createdAt,
    required this.updatedAt,
    this.changementMdpObligatoire = false,
    this.nom,
    this.prenom,
    this.statut,
    this.telephone,
    this.photoUrl,
    this.adresse,
    this.ville,
    this.codePostal,
    this.relaisId,
    this.relaisNom,
    this.numeroDossier,
    this.dateNaissance,
    this.lieuNaissanceVille,
    this.lieuNaissancePays,
  });

  static String _str(dynamic v) {
    if (v == null) return '';
    if (v is String) return v;
    return v.toString();
  }

  static DateTime _date(dynamic v) {
    if (v == null) return DateTime.now();
    if (v is DateTime) return v;
    try {
      return DateTime.parse(v.toString());
    } catch (_) {
      return DateTime.now();
    }
  }

  factory AppUser.fromJson(Map<String, dynamic> json) {
    final relaisJson = json['relais'];
    final relaisMap =
        relaisJson is Map<String, dynamic> ? relaisJson : <String, dynamic>{};

    return AppUser(
      id: _str(json['id']),
      email: _str(json['email']),
      role: _str(json['role']),
      createdAt: _date(json['cree_le'] ?? json['createdAt']),
      updatedAt: _date(json['modifie_le'] ?? json['updatedAt']),
      changementMdpObligatoire:
          json['changement_mdp_obligatoire'] == true,
      nom: json['nom'] is String ? json['nom'] as String : null,
      prenom: json['prenom'] is String ? json['prenom'] as String : null,
      statut: json['statut'] is String ? json['statut'] as String : null,
      telephone: json['telephone'] is String ? json['telephone'] as String : null,
      photoUrl: json['photo_url'] is String ? json['photo_url'] as String : null,
      adresse: json['adresse'] is String ? json['adresse'] as String : null,
      ville: json['ville'] is String ? json['ville'] as String : null,
      codePostal: json['code_postal'] is String ? json['code_postal'] as String : null,
      relaisId: (json['relaisId'] ?? json['relais_id'] ?? relaisMap['id'])
          ?.toString(),
      relaisNom: relaisMap['nom']?.toString(),
      numeroDossier: json['numero_dossier'] is String ? json['numero_dossier'] as String : null,
      dateNaissance: _optionalDateString(json['date_naissance']),
      lieuNaissanceVille: json['lieu_naissance_ville'] is String
          ? json['lieu_naissance_ville'] as String
          : null,
      lieuNaissancePays: json['lieu_naissance_pays'] is String
          ? json['lieu_naissance_pays'] as String
          : null,
    );
  }

  static String? _optionalDateString(dynamic v) {
    if (v == null) return null;
    if (v is String) return v.isEmpty ? null : v;
    if (v is DateTime) return v.toIso8601String().split('T').first;
    final s = v.toString();
    return s.isEmpty ? null : s;
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'email': email,
      'role': role,
      'createdAt': createdAt.toIso8601String(),
      'updatedAt': updatedAt.toIso8601String(),
      'changement_mdp_obligatoire': changementMdpObligatoire,
      'nom': nom,
      'prenom': prenom,
      'statut': statut,
      'telephone': telephone,
      'photo_url': photoUrl,
      'adresse': adresse,
      'ville': ville,
      'code_postal': codePostal,
      'relais_id': relaisId,
      'relais_nom': relaisNom,
      'numero_dossier': numeroDossier,
      'date_naissance': dateNaissance,
      'lieu_naissance_ville': lieuNaissanceVille,
      'lieu_naissance_pays': lieuNaissancePays,
    };
  }

  String get fullName => '${prenom ?? ''} ${nom ?? ''}'.trim();
}
