package com.fabledsword.minstrel.shared.widgets

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.composables.icons.lucide.Lucide
import com.composables.icons.lucide.Music

/**
 * Shared track-row cover thumbnail. Pulls the cover from the track's
 * derived `coverUrl` (which Coil resolves through the shared OkHttp
 * + BaseUrlInterceptor); falls back to a Lucide.Music glyph on a
 * surfaceVariant-tinted background when the URL is empty (album
 * cover hasn't loaded into the cache yet OR the track has no album
 * binding).
 *
 * Used by LikedTab, PlaylistDetail, HistoryTab, and any other dense
 * track list. Sizes default to 48dp — the four-corner radius and
 * fallback-icon size scale with the outer dimension so callers can
 * pass e.g. 56dp for a more prominent row without re-implementing.
 */
@Composable
fun TrackCoverThumb(
    coverUrl: String,
    contentDescription: String?,
    modifier: Modifier = Modifier,
    size: Dp = DEFAULT_SIZE,
) {
    Box(
        modifier = modifier
            .size(size)
            .clip(RoundedCornerShape(4.dp))
            .background(MaterialTheme.colorScheme.surfaceVariant),
        contentAlignment = Alignment.Center,
    ) {
        ServerImage(
            url = coverUrl,
            contentDescription = contentDescription,
            modifier = Modifier.fillMaxSize(),
        ) {
            Icon(
                imageVector = Lucide.Music,
                contentDescription = null,
                tint = MaterialTheme.colorScheme.onSurfaceVariant,
                modifier = Modifier.size(size / 2),
            )
        }
    }
}

private val DEFAULT_SIZE: Dp = 48.dp
