class AppNotification {
  final String id;
  final String title;
  final String? description;
  final String type; // 'PUBLIC' | 'JOIN_REQUEST'
  final String? groupId;
  final bool? isAccepted;
  final String receiverId;
  final String? senderId;

  const AppNotification({
    required this.id,
    required this.title,
    this.description,
    required this.type,
    this.groupId,
    this.isAccepted,
    required this.receiverId,
    this.senderId,
  });

  factory AppNotification.fromJson(Map<String, dynamic> json) {
    final rawType = (json['type'] as String?) ?? 'PUBLIC';
    final normalizedType = _normalizeType(rawType);
    return AppNotification(
      id: json['id'].toString(),
      title: json['title'] as String,
      description: json['description'] as String?,
      type: normalizedType,
      groupId: json['group_id']?.toString(),
      isAccepted: json['is_accepted'] as bool?,
      receiverId: json['receiver_id'].toString(),
      senderId: json['sender_id']?.toString(),
    );
  }

  static String _normalizeType(String rawType) {
    final upper = rawType.trim().toUpperCase();
    if (upper == 'GROUP_INVITATION' ||
        upper == 'GROUP_INVITE' ||
        upper == 'INVITE' ||
        upper == 'JOIN_REQUEST') {
      return 'JOIN_REQUEST';
    }
    return upper;
  }

  bool get isJoinRequest => type == 'JOIN_REQUEST';
  bool get isPending => isAccepted == null;
}
