package com.fabledsword.minstrel.models

import kotlinx.serialization.Serializable

/**
 * Lightweight reference to one track. Mirrors
 * `flutter_client/lib/models/track.dart`'s `TrackRef`.
 *
 * The `Ref` suffix matches the Flutter convention — these types carry
 * only the IDs + display fields needed for list rendering + the player
 * queue, not full per-row metadata.
 *
 * Constructed from `TrackWire` (server JSON) via `LibraryMappers`, and
 * from `CachedTrackEntity` (Room cache) via the same mappers. Either
 * source produces an equivalent TrackRef.
 *
 * `@Serializable` because the ResumeController persists List<TrackRef>
 * as JSON in `cached_resume_state.json`. (Mild leak of persistence
 * concern into the domain type, but the alternative — a duplicate
 * persisted DTO with manual mappers — wasn't worth the boilerplate.)
 */
@Serializable
data class TrackRef(
    val id: String,
    val title: String,
    val albumId: String,
    val artistId: String,
    val albumTitle: String = "",
    val artistName: String = "",
    val trackNumber: Int? = null,
    val discNumber: Int? = null,
    val durationSec: Int = 0,
    val streamUrl: String = "",
) {
    /**
     * Cover URL derived from the parent album's `/api/albums/{id}/cover`
     * endpoint. Uses the placeholder host that `BaseUrlInterceptor`
     * rewrites to the live server URL — Coil shares the same OkHttp
     * client as Retrofit so the rewrite + auth-cookie flow applies
     * identically. Empty `albumId` yields an empty string; callers
     * branch on isEmpty to show the placeholder icon.
     */
    val coverUrl: String
        get() = albumCoverPath(albumId)
}
