// Mirrors internal/api/types.go AlbumRef and web/src/lib/api/types.ts.
//
// `cover_url` (NOT cover_art_url) and `duration_sec` (NOT duration_ms) match
// the server contract. `year` is omitempty on the server so we keep it
// nullable. CoverURL is non-null but may be "" — UI branches on isEmpty.
class AlbumRef {
  const AlbumRef({
    required this.id,
    required this.title,
    required this.artistId,
    this.sortTitle = '',
    this.artistName = '',
    this.year,
    this.trackCount = 0,
    this.durationSec = 0,
    this.coverUrl = '',
  });

  final String id;
  final String title;
  final String sortTitle;
  final String artistId;
  final String artistName;
  final int? year;
  final int trackCount;
  final int durationSec;
  final String coverUrl;

  factory AlbumRef.fromJson(Map<String, dynamic> j) => AlbumRef(
        id: j['id'] as String,
        title: j['title'] as String,
        sortTitle: j['sort_title'] as String? ?? '',
        artistId: j['artist_id'] as String,
        artistName: j['artist_name'] as String? ?? '',
        year: (j['year'] as num?)?.toInt(),
        trackCount: (j['track_count'] as num?)?.toInt() ?? 0,
        durationSec: (j['duration_sec'] as num?)?.toInt() ?? 0,
        coverUrl: j['cover_url'] as String? ?? '',
      );
}
