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

/// Mirrors internal/api/search.go SearchResponse — three pages keyed by
/// facet, each independently paged. The mobile UI usually only walks
/// the first page of each facet (limit 20-50); infinite scroll within a
/// facet is a future enhancement, not v1.
class SearchResponse {
  const SearchResponse({
    required this.artists,
    required this.albums,
    required this.tracks,
  });

  final Paged<ArtistRef> artists;
  final Paged<AlbumRef> albums;
  final Paged<TrackRef> tracks;

  factory SearchResponse.fromJson(Map<String, dynamic> j) => SearchResponse(
        artists: Paged.fromJson(
          (j['artists'] as Map?)?.cast<String, dynamic>() ?? const {},
          ArtistRef.fromJson,
        ),
        albums: Paged.fromJson(
          (j['albums'] as Map?)?.cast<String, dynamic>() ?? const {},
          AlbumRef.fromJson,
        ),
        tracks: Paged.fromJson(
          (j['tracks'] as Map?)?.cast<String, dynamic>() ?? const {},
          TrackRef.fromJson,
        ),
      );

  bool get isEmpty =>
      artists.items.isEmpty && albums.items.isEmpty && tracks.items.isEmpty;
}
