import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:em2rp/models/notification_preferences_model.dart';

class UserModel {
  final String uid;
  final String firstName;
  final String lastName;
  final String role;
  final String profilePhotoUrl;
  final String email;
  final String phoneNumber;
  final NotificationPreferences? notificationPreferences;

  UserModel({
    required this.uid,
    required this.firstName,
    required this.lastName,
    required this.role,
    required this.profilePhotoUrl,
    required this.email,
    required this.phoneNumber,
    this.notificationPreferences,
  });

  // Convertit une Map (Firestore) en UserModel
  factory UserModel.fromMap(Map<String, dynamic> data, String uid) {
    String roleString;
    final roleField = data['role'];

    if (roleField is String) {
      // Cas 1 : role est déjà un String (ex: "roles/ADMIN")
      roleString = roleField;
    } else if (roleField is DocumentReference) {
      // Cas 2 : role est une DocumentReference
      roleString = roleField.id;
    } else if (roleField is Map) {
      // Cas 3 : role est un Map sérialisé (ex: {"_path": {"segments": ["roles", "ADMIN"]}})
      // On extrait le path
      final pathData = roleField['_path'];
      if (pathData is Map && pathData['segments'] is List) {
        final segments = pathData['segments'] as List;
        if (segments.length >= 2) {
          roleString = segments[1].toString(); // Ex: "ADMIN"
        } else {
          roleString = 'USER';
        }
      } else {
        roleString = 'USER';
      }
    } else {
      // Cas par défaut
      roleString = 'USER';
    }

    return UserModel(
      uid: uid,
      firstName: data['firstName'] ?? '',
      lastName: data['lastName'] ?? '',
      role: roleString,
      profilePhotoUrl: data['profilePhotoUrl'] ?? '',
      email: data['email'] ?? '',
      phoneNumber: data['phoneNumber'] ?? '',
      notificationPreferences: data['notificationPreferences'] != null
          ? NotificationPreferences.fromMap(data['notificationPreferences'] as Map<String, dynamic>)
          : NotificationPreferences.defaults(),
    );
  }

  // Convertit un UserModel en Map pour Firestore
  Map<String, dynamic> toMap() {
    return {
      'firstName': firstName,
      'lastName': lastName,
      'role': role, // Envoyer directement le string roleId au lieu de créer une DocumentReference
      'profilePhotoUrl': profilePhotoUrl,
      'email': email,
      'phoneNumber': phoneNumber,
      if (notificationPreferences != null)
        'notificationPreferences': notificationPreferences!.toMap(),
    };
  }

  UserModel copyWith({
    String? firstName,
    String? lastName,
    String? role,
    String? profilePhotoUrl,
    String? email,
    String? phoneNumber,
    NotificationPreferences? notificationPreferences,
  }) {
    return UserModel(
      uid: uid, // L'UID ne change pas
      firstName: firstName ?? this.firstName,
      lastName: lastName ?? this.lastName,
      role: role ?? this.role,
      profilePhotoUrl: profilePhotoUrl ?? this.profilePhotoUrl,
      email: email ?? this.email,
      phoneNumber: phoneNumber ?? this.phoneNumber,
      notificationPreferences: notificationPreferences ?? this.notificationPreferences,
    );
  }
}
