package com.fabledsword.minstrel.player

import com.fabledsword.minstrel.auth.AuthStore
import com.fabledsword.minstrel.cache.audiocache.CacheConfig
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

/**
 * Player + audio-cache wiring. PlayerFactory has @Inject so it doesn't
 * need a @Provides here; this module exists to provide the
 * configuration values (CacheConfig) it depends on, and any future
 * player-related singletons that aren't @Inject-constructible.
 *
 * MinstrelPlayerService (Phase 6.2) injects PlayerFactory and calls
 * `build()` from `onCreate`. The eviction worker (Phase 12.3) injects
 * PlayerFactory too and reads `simpleCache` from it.
 */
@Module
@InstallIn(SingletonComponent::class)
object PlayerModule {

    /**
     * CacheConfig snapshot from the user's persisted CacheSettings.
     * SimpleCache is constructed once per process, so changes via the
     * Settings UI take effect on next app launch (not mid-flight).
     * AuthStore's StateFlow hydrates async from Room — when the
     * factory @Provides runs, the value is either the persisted
     * setting (if Room finished first) or CacheSettings.DEFAULT.
     * Worst case the user's tuning lands on next-next launch, which
     * is acceptable for a single-user device pref.
     */
    @Provides
    @Singleton
    fun provideCacheConfig(authStore: AuthStore): CacheConfig {
        val s = authStore.cacheSettings.value
        return CacheConfig(
            likedCapBytes = s.likedCapBytes,
            rollingCapBytes = s.rollingCapBytes,
        )
    }
}
