package com.example.livingai.pages.components

import android.net.Uri
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import coil.compose.rememberAsyncImagePainter
import coil.request.ImageRequest
import kotlin.math.roundToInt

@Composable
fun ThumbnailImage(
    imageUri: Uri,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    thumbnailSize: Dp = 120.dp,
    applySizeModifier: Boolean = true,
    requestSizePx: Int? = null
) {
    val context = LocalContext.current
    val sizeInPx = requestSizePx ?: with(LocalDensity.current) { thumbnailSize.toPx().roundToInt() }

    var finalModifier = modifier.clickable { onClick() }
    if (applySizeModifier) {
        finalModifier = finalModifier.size(thumbnailSize)
    }

    Image(
        painter = rememberAsyncImagePainter(
            ImageRequest.Builder(context)
                .data(imageUri)
                .size(sizeInPx)
                .crossfade(true)
                .build()
        ),
        contentDescription = "Thumbnail",
        modifier = finalModifier,
        contentScale = ContentScale.Crop
    )
}
