package com.fabledsword.minstrel.models

/**
 * Lightweight reference to one artist. Mirrors
 * `flutter_client/lib/models/artist.dart`'s `ArtistRef`.
 *
 * `coverUrl` is the server's field name (NOT cover_art_url). Server emits
 * empty string when the artist has no representative album cover; UI code
 * branches on isEmpty rather than null-checking.
 */
data class ArtistRef(
    val id: String,
    val name: String,
    val sortName: String = "",
    val albumCount: Int = 0,
    val coverUrl: String = "",
    val coverAlbumId: String = "",
) {
    /**
     * Cover URL the UI should render, or null when the artist has no
     * representative cover. Prefers the server-provided `coverUrl`
     * (relative, e.g. `/api/artists` ships it); otherwise derives the
     * first cached album's `/api/albums/{id}/cover` via [coverAlbumId].
     *
     * The fallback matters because cached_artists stores no cover and
     * the sync payload carries none — so for cache-sourced artist tiles
     * we mirror Flutter's `artistTileProvider`, which JOINs the first
     * album to supply the cover. Resolve through `ServerImage`.
     */
    val displayCoverUrl: String?
        get() = when {
            coverUrl.isNotEmpty() -> coverUrl
            coverAlbumId.isNotEmpty() -> albumCoverPath(coverAlbumId)
            else -> null
        }
}
