import '/core/constants.dart';
import 'package:chepuhagram/data/models/message_model.dart';

class Contact {
  final int id;
  final String username;
  String name;
  String surname;
  final String? lastMessage;
  String? avatarFileId;
  String? avatarUrl;
  final DateTime? lastMessageTime;
  final bool isOnline;
  final int unreadCount;
  String? publicKey;
  final bool isLastMsgDecrypted;
  final int? lastMessageId;
  final MessageType? lastMessageType;

  String? get effectiveAvatarUrl {
  if (avatarFileId != null && avatarFileId!.isNotEmpty) {
    return '${AppConstants.baseUrl}/media/$avatarFileId';
  }
  return avatarUrl;
}

  Contact({
    required this.id,
    required this.username,
    required this.name,
    required this.surname,  
    this.lastMessage,
    this.avatarFileId,
    this.avatarUrl,
    this.lastMessageTime,
    this.isOnline = false,
    this.unreadCount = 0,
    this.publicKey,
    this.isLastMsgDecrypted = false,
    this.lastMessageId,
    this.lastMessageType,
  });

  Contact copyWith({
    int? id,
    String? username,
    String? name,
    String? surname,
    String? lastMessage,
    String? avatarFileId,
    String? avatarUrl,
    DateTime? lastMessageTime,
    bool? isOnline,
    int? unreadCount,
    String? publicKey,
    bool? isLastMsgDecrypted,
    int? lastMessageId,
    MessageType? lastMessageType,
  }) {
    return Contact(
      id: id ?? this.id,
      username: username ?? this.username,
      name: name ?? this.name,
      surname: surname ?? this.surname,
      lastMessage: lastMessage ?? this.lastMessage,
      avatarFileId: avatarFileId ?? this.avatarFileId,
      avatarUrl: avatarUrl ?? this.avatarUrl,
      lastMessageTime: lastMessageTime ?? this.lastMessageTime,
      isOnline: isOnline ?? this.isOnline,
      unreadCount: unreadCount ?? this.unreadCount,
      publicKey: publicKey ?? this.publicKey,
      isLastMsgDecrypted: isLastMsgDecrypted ?? this.isLastMsgDecrypted,
      lastMessageId: lastMessageId ?? this.lastMessageId,
      lastMessageType: lastMessageType ?? this.lastMessageType,
    );
  }

  factory Contact.fromJson(Map<String, dynamic> json) {
    DateTime? parseTime(dynamic value) {
      if (value == null) return null;
      if (value is DateTime) return value;
      final asString = value.toString();
      return DateTime.tryParse(asString);
    }

    return Contact(
      id: json['id'],
      username: json['username'] ?? 'Unknown',
      name: json['name'] ?? json['first_name'] ?? 'Unknown',
      surname: json['surname'] ?? json['last_name'] ?? 'Unknown',
      lastMessage: json['last_message'] ?? json['lastMessage'],
      avatarFileId: json['avatar_file_id'] ?? json['avatarFileId'],
      avatarUrl: json['avatar_url'] ?? json['avatarUrl'],
      lastMessageTime: parseTime(json['last_message_time'] ?? json['lastMessageTime']),
      isOnline: (json['online'] ?? json['Online']) == true,
      unreadCount: int.tryParse((json['unread_count'] ?? json['unreadCount'] ?? 0).toString()) ?? 0,
      publicKey: json['public_key'],
      isLastMsgDecrypted: json['is_last_msg_decrypted'] ?? false,
      lastMessageId: int.tryParse((json['last_message_id'] ?? json['lastMessageId'] ?? 0).toString()) ?? 0,
      lastMessageType: MessageModel.parseMessageType(json['last_message_type'] ?? json['lastMessageType'] ?? 'text'),
    );
  }
}
