package com.fabledsword.minstrel.player

import com.fabledsword.minstrel.api.endpoints.CastApi
import com.fabledsword.minstrel.api.endpoints.StreamTokenRequest
import com.fabledsword.minstrel.api.endpoints.StreamTokenResponse
import retrofit2.Retrofit
import retrofit2.create
import javax.inject.Inject
import javax.inject.Singleton

/**
 * Mints stream tokens for a given track id. Pulled into its own Hilt
 * singleton so [MinstrelForwardingPlayer] (service-side) and
 * [com.fabledsword.minstrel.player.output.OutputPickerController]
 * (controller-side) don't each construct their own [CastApi] from
 * Retrofit. The shared Retrofit instance is unchanged.
 */
@Singleton
class StreamTokenProvider @Inject constructor(retrofit: Retrofit) {
    private val api: CastApi = retrofit.create()

    suspend fun mint(trackId: String): StreamTokenResponse =
        api.streamToken(StreamTokenRequest(trackId = trackId))
}
