// Snapshots use only model fields referenced by the supplied news screens.
class _Snapshot {
  final Map<String,dynamic> data;
  _Snapshot(Map value):data=Map<String,dynamic>.from(value);
  String text(String key)=>'${data[key]??''}';
  int number(String key)=>(data[key] as num? ?? 0).toInt();
  DateTime date(String key)=>DateTime.parse(text(key));
  DateTime? optionalDate(String key)=>data[key]==null?null:date(key);
}
String _optionalPoster(dynamic video){
  try{return '${video.thumbnailPath??''}';}catch(_){}
  try{return '${video.posterPath??''}';}catch(_){}
  return '';
}
bool _remoteIsTrending(dynamic article){
  try{return article.isTrending==true;}catch(_){}
  try{return article.isTrending==1||article.isTrending=='1'||article.isTrending=='t';}catch(_){}
  try{return article.is_trending==true||article.is_trending==1||article.is_trending=='1'||article.is_trending=='t';}catch(_){}
  try{final map=article.toJson() as Map;return map['is_trending']==true||map['is_trending']==1||map['is_trending']=='1'||map['is_trending']=='t';}catch(_){}
  return false;
}
class CampusNewsArticle extends _Snapshot {
  CampusNewsArticle(Map value):super(value);
  factory CampusNewsArticle.remote(dynamic v)=>CampusNewsArticle({
    'title':v.title,
    'body':v.body,
    'authorName':v.authorName,
    'authorProfilePicture':v.authorProfilePicture,
    'authorRole':v.authorRole,
    'campus':v.campus,
    'category':v.category,
    'id':v.id,
    'authorId':v.authorId,
    'likeCount':v.likeCount,
    'commentCount':v.commentCount,
    'createdAt':(v.createdAt as DateTime).toIso8601String(),
    'publishedAt':(v.publishedAt as DateTime?)?.toIso8601String(),
    'updatedAt':(v.updatedAt as DateTime?)?.toIso8601String(),
    'likedByMe':v.likedByMe,
    'isTrending':_remoteIsTrending(v),
    'images':[for(final i in v.images)CampusNewsImage.remote(i).data],
    'video':v.video==null?null:CampusNewsVideo.remote(v.video).data,
  });
  String get title=>text('title');
  String get body=>text('body');
  String get authorName=>text('authorName');
  String get authorProfilePicture=>text('authorProfilePicture');
  String get authorRole=>text('authorRole');
  String get campus=>text('campus');
  String get category=>text('category');
  int get id=>number('id');
  int get authorId=>number('authorId');
  int get likeCount=>number('likeCount');
  int get commentCount=>number('commentCount');
  DateTime get createdAt=>date('createdAt');
  DateTime? get publishedAt=>optionalDate('publishedAt');
  DateTime? get updatedAt=>optionalDate('updatedAt');
  bool get likedByMe=>data['likedByMe']==true;
  bool get isTrending=>data['isTrending']==true;
  List<CampusNewsImage> get images=>(data['images'] as List? ?? []).whereType<Map>().map(CampusNewsImage.new).toList();
  CampusNewsVideo? get video=>data['video']==null?null:CampusNewsVideo(data['video'] as Map);
  CampusNewsArticle copyWith({bool? likedByMe,int? likeCount,int? commentCount})=>CampusNewsArticle({...data,
    if(likedByMe!=null)'likedByMe':likedByMe,if(likeCount!=null)'likeCount':likeCount,if(commentCount!=null)'commentCount':commentCount});
}
class CampusNewsImage extends _Snapshot {
  CampusNewsImage(Map value):super(value);
  factory CampusNewsImage.remote(dynamic v)=>CampusNewsImage({
    'path':v.path,
    'previewPath':v.previewPath,
    'id':v.id,
  });
  String get path=>text('path');
  String get previewPath=>text('previewPath');
  int get id=>number('id');
}
class CampusNewsVideo extends _Snapshot {
  CampusNewsVideo(Map value):super(value);
  factory CampusNewsVideo.remote(dynamic v)=>CampusNewsVideo({
    'path':v.path,
    'fileName':v.fileName,
    'formattedSize':v.formattedSize,
    'id':v.id,
    'posterPath':_optionalPoster(v),
  });
  String get path=>text('path');
  String get fileName=>text('fileName');
  String get formattedSize=>text('formattedSize');
  int get id=>number('id');
  String get posterPath=>text('posterPath');
}
class CampusNewsComment extends _Snapshot {
  CampusNewsComment(Map value):super(value);
  factory CampusNewsComment.remote(dynamic v)=>CampusNewsComment({
    'authorName':v.authorName,
    'authorProfilePicture':v.authorProfilePicture,
    'comment':v.comment,
    'replyToAuthorName':v.replyToAuthorName,
    'id':v.id,
    'userId':v.userId,
    'createdAt':(v.createdAt as DateTime).toIso8601String(),
    'parentCommentId':v.parentCommentId,
  });
  String get authorName=>text('authorName');
  String get authorProfilePicture=>text('authorProfilePicture');
  String get comment=>text('comment');
  String get replyToAuthorName=>text('replyToAuthorName');
  int get id=>number('id');
  int get userId=>number('userId');
  DateTime get createdAt=>date('createdAt');
  int? get parentCommentId=>(data['parentCommentId'] as num?)?.toInt();
}
class CampusNewsNotification extends _Snapshot {
  CampusNewsNotification(Map value):super(value);
  factory CampusNewsNotification.remote(dynamic v)=>CampusNewsNotification({
    'actorName':v.actorName,
    'actorProfilePicture':v.actorProfilePicture,
    'articleTitle':v.articleTitle,
    'commentPreview':v.commentPreview,
    'type':v.type,
    'id':v.id,
    'newsId':v.newsId,
    'createdAt':(v.createdAt as DateTime).toIso8601String(),
    'isRead':v.isRead,
  });
  String get actorName=>text('actorName');
  String get actorProfilePicture=>text('actorProfilePicture');
  String get articleTitle=>text('articleTitle');
  String get commentPreview=>text('commentPreview');
  String get type=>text('type');
  int get id=>number('id');
  int get newsId=>number('newsId');
  DateTime get createdAt=>date('createdAt');
  bool get isRead=>data['isRead']==true;
  CampusNewsNotification copyWith({bool? isRead})=>CampusNewsNotification({...data,if(isRead!=null)'isRead':isRead});
}
class NewsMentionUser extends _Snapshot {
  NewsMentionUser(Map value):super(value);
  factory NewsMentionUser.remote(dynamic v)=>NewsMentionUser({
    'fullName':v.fullName,
    'profilePicture':v.profilePicture,
    'id':v.id,
  });
  String get fullName=>text('fullName');
  String get profilePicture=>text('profilePicture');
  int get id=>number('id');
}
class CampusNewsPage {final List<CampusNewsArticle> articles;final bool hasMore;CampusNewsPage(this.articles,this.hasMore);}
class NewsNotificationPage {final List<CampusNewsNotification> notifications;final int unreadCount;NewsNotificationPage(this.notifications,this.unreadCount);}
