package com.fabledsword.minstrel.cache.db.entities

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.fabledsword.minstrel.cache.db.CacheSource
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant

/**
 * One row per fully-downloaded audio file. Mirrors
 * `flutter_client/lib/cache/db.dart`'s `AudioCacheIndex` Drift table.
 *
 * Drives the 2-bucket LRU eviction (Phase 12 AudioCacheEvictionWorker):
 *   - `incidental` files (streamed-and-cached side effect) evict first
 *   - `autoPrefetch` second
 *   - `manual` (user explicitly pinned) evict last
 *
 * `cachedAt` = when we downloaded. `lastPlayedAt` = when the user last
 * played it; drives the offline "recently played" ordering and is the
 * tiebreaker for LRU eviction within a source bucket. Nullable on
 * brand-new rows until the first post-cache play touches them.
 */
@Entity(tableName = "audio_cache_index")
data class AudioCacheIndexEntity(
    @PrimaryKey val trackId: String,
    val path: String,
    val sizeBytes: Int,
    val cachedAt: Instant = Clock.System.now(),
    val lastPlayedAt: Instant? = null,
    val source: CacheSource,
)
