package com.fabledsword.minstrel.player

import com.fabledsword.minstrel.api.endpoints.RadioApi
import com.fabledsword.minstrel.library.data.toDomain
import com.fabledsword.minstrel.models.TrackRef
import retrofit2.Retrofit
import retrofit2.create
import javax.inject.Inject
import javax.inject.Singleton

/**
 * Thin facade over `/api/radio` that maps the server response into
 * the domain `TrackRef` list `PlayerController.setQueue` consumes.
 * Owning the mapping here keeps PlayerController free of Retrofit
 * + wire-type imports; the player only sees `List<TrackRef>`.
 *
 * Construction follows the project convention from NetworkModule:
 * per-endpoint Retrofit interfaces aren't @Provides-bound; consumers
 * inject the shared `Retrofit` and call `.create()` themselves.
 */
@Singleton
class RadioController @Inject constructor(retrofit: Retrofit) {
    private val api: RadioApi = retrofit.create()

    /**
     * Calls `GET /api/radio?seed_track=<trackId>` and maps the
     * response into domain TrackRefs. Returns an empty list on a
     * 2xx-but-empty server response; throws on any transport or
     * non-2xx status (caller surfaces the error).
     */
    suspend fun seed(trackId: String): List<TrackRef> =
        api.seedTrack(trackId).tracks.map { it.toDomain() }
}
