package com.fabledsword.minstrel.shared.widgets

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import coil3.compose.AsyncImage
import com.fabledsword.minstrel.shared.resolveServerUrl

/**
 * Renders a server-hosted image, resolving relative URLs centrally so
 * every cover surface loads consistently. Shows [fallback] when the URL
 * is blank or unresolvable.
 */
@Composable
fun ServerImage(
    url: String?,
    contentDescription: String?,
    modifier: Modifier = Modifier,
    contentScale: ContentScale = ContentScale.Crop,
    fallback: @Composable () -> Unit = {},
) {
    val resolved = resolveServerUrl(url)
    if (resolved == null) {
        fallback()
    } else {
        AsyncImage(
            model = resolved,
            contentDescription = contentDescription,
            modifier = modifier,
            contentScale = contentScale,
        )
    }
}
