package com.fabledsword.minstrel.library.widgets

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.composables.icons.lucide.Disc3
import com.composables.icons.lucide.Lucide
import androidx.compose.material3.Icon
import com.fabledsword.minstrel.models.AlbumRef
import com.fabledsword.minstrel.nav.LocalDetailSeedCache
import com.fabledsword.minstrel.shared.widgets.CoverTile
import com.fabledsword.minstrel.shared.widgets.PlayCircleButton
import androidx.compose.ui.Alignment

/**
 * One album tile. Mirrors the Flutter `AlbumCard` (~176dp wide, 144dp
 * square cover). Tap fires `onClick` — wired to `AlbumDetail` nav in
 * Phase 5.5. The tile stashes its `album` in [LocalDetailSeedCache]
 * before firing onClick so the detail screen's AppBar renders title /
 * cover / counts immediately without waiting for the fetch.
 *
 * [onPlay] is an opt-in play-overlay handler. When non-null, a 44dp
 * circular play button is overlaid at the bottom-right of the cover
 * (mirrors Flutter's PlayCircleButton). Only Home wires it currently;
 * Library / Discover / detail surfaces leave it null.
 */
@Composable
fun AlbumCard(
    album: AlbumRef,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    onPlay: (suspend () -> Unit)? = null,
) {
    val seedCache = LocalDetailSeedCache.current
    Surface(
        modifier = modifier
            .width(176.dp)
            .clickable {
                seedCache.stashAlbum(album)
                onClick()
            },
        color = Color.Transparent,
    ) {
        Column(modifier = Modifier.padding(horizontal = 8.dp)) {
            CoverTile(
                url = album.displayCoverUrl,
                contentDescription = album.title,
                size = 144.dp,
                background = MaterialTheme.colorScheme.surfaceVariant,
                fallback = {
                    Icon(
                        imageVector = Lucide.Disc3,
                        contentDescription = null,
                        tint = MaterialTheme.colorScheme.onSurfaceVariant,
                        modifier = Modifier.size(56.dp),
                    )
                },
                overlay = {
                    if (onPlay != null) {
                        PlayCircleButton(
                            onPlay = onPlay,
                            modifier = Modifier
                                .align(Alignment.BottomEnd)
                                .padding(6.dp),
                        )
                    }
                },
            )
            Spacer(Modifier.height(8.dp))
            Text(
                text = album.title,
                style = MaterialTheme.typography.titleSmall,
                color = MaterialTheme.colorScheme.onSurface,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
            )
            Text(
                text = album.artistName.ifEmpty { " " },
                style = MaterialTheme.typography.bodySmall,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
            )
        }
    }
}
