import 'album.dart';
import 'artist.dart';
import 'track.dart';

// Mirrors internal/api/types.go HomePayload and web/src/lib/api/types.ts.
// Section keys MUST stay in sync with the server's JSON tags:
//   recently_added_albums / rediscover_albums / rediscover_artists /
//   most_played_tracks / last_played_artists.
//
// Server contract guarantees all five slices are non-nil at JSON encode
// time (empty sections render as []), but we still tolerate missing keys
// so partial fixtures and offline-degraded responses can be parsed.
class HomeData {
  const HomeData({
    required this.recentlyAddedAlbums,
    required this.rediscoverAlbums,
    required this.rediscoverArtists,
    required this.mostPlayedTracks,
    required this.lastPlayedArtists,
  });

  final List<AlbumRef> recentlyAddedAlbums;
  final List<AlbumRef> rediscoverAlbums;
  final List<ArtistRef> rediscoverArtists;
  final List<TrackRef> mostPlayedTracks;
  final List<ArtistRef> lastPlayedArtists;

  factory HomeData.fromJson(Map<String, dynamic> j) => HomeData(
        recentlyAddedAlbums: _list(j, 'recently_added_albums', AlbumRef.fromJson),
        rediscoverAlbums:    _list(j, 'rediscover_albums',     AlbumRef.fromJson),
        rediscoverArtists:   _list(j, 'rediscover_artists',    ArtistRef.fromJson),
        mostPlayedTracks:    _list(j, 'most_played_tracks',    TrackRef.fromJson),
        lastPlayedArtists:   _list(j, 'last_played_artists',   ArtistRef.fromJson),
      );

  static List<T> _list<T>(
    Map<String, dynamic> j,
    String key,
    T Function(Map<String, dynamic>) parse,
  ) {
    final raw = j[key] as List? ?? const [];
    return raw
        .map((e) => parse((e as Map).cast<String, dynamic>()))
        .toList(growable: false);
  }
}
