class MediaItem {
  final int id;
  final String mime;
  final int size;
  final int stamp;
  final String dest;
  final int mode;
  final List<Tag>? tags;
  final List<Favorite>? favorites;

  MediaItem({
    required this.id,
    required this.mime,
    required this.size,
    required this.stamp,
    required this.dest,
    required this.mode,
    this.tags = const [],
    this.favorites = const [],
  });

  String get thumbnailUrl => 'https://f0ck.me/t/$id.webp';
  String get mediaUrl => 'https://f0ck.me/b/$dest';
  String get coverUrl => 'https://f0ck.me/ca/$id.webp';
  String get postUrl => 'https://f0ck.me/$id';

  MediaItem copyWith({
    int? id,
    String? mime,
    int? size,
    int? stamp,
    String? dest,
    int? mode,
    List<Tag>? tags,
    List<Favorite>? favorites,
  }) {
    return MediaItem(
      id: id ?? this.id,
      mime: mime ?? this.mime,
      size: size ?? this.size,
      stamp: stamp ?? this.stamp,
      dest: dest ?? this.dest,
      mode: mode ?? this.mode,
      tags: tags ?? this.tags,
      favorites: favorites ?? this.favorites,
    );
  }

  factory MediaItem.fromJson(Map<String, dynamic> json) {
    return MediaItem(
      id: json['id'],
      mime: json['mime'],
      size: json['size'],
      stamp: json['stamp'],
      dest: json['dest'],
      mode: json['mode'],
      tags:
          (json['tags'] as List<dynamic>?)
              ?.map((e) => Tag.fromJson(e))
              .toList() ??
          [],
      favorites:
          (json['favorites'] as List<dynamic>?)
              ?.map((e) => Favorite.fromJson(e))
              .toList() ??
          [],
    );
  }
}

class Tag {
  final int id;
  final String tag;
  final String normalized;

  Tag({required this.id, required this.tag, required this.normalized});

  factory Tag.fromJson(Map<String, dynamic> json) {
    return Tag(
      id: json['id'],
      tag: json['tag'],
      normalized: json['normalized'],
    );
  }
}

class Favorite {
  final int userId;
  final String username;
  final int avatar;

  Favorite({
    required this.userId,
    required this.username,
    required this.avatar,
  });

  factory Favorite.fromJson(Map<String, dynamic> json) {
    return Favorite(
      userId: json['user_id'],
      username: json['user'],
      avatar: json['avatar'],
    );
  }

  String get avatarUrl => 'https://f0ck.me/t/$avatar.webp';
}
