package com.fabledsword.minstrel.playlists.widgets

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
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.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
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.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.composables.icons.lucide.ListMusic
import com.composables.icons.lucide.Lucide
import com.composables.icons.lucide.RefreshCw
import com.composables.icons.lucide.TriangleAlert
import com.fabledsword.minstrel.theme.FabledSwordFlatTokens

/**
 * Placeholder tile for a system playlist that hasn't generated yet.
 * Mirrors `flutter_client/lib/playlists/widgets/playlist_placeholder_card.dart`.
 * Sized to match [PlaylistCard] so the Home Playlists row stays
 * visually consistent.
 *
 * [variant] is one of building / failed / pending / seed-needed,
 * derived by the caller from the system-playlists build status:
 * - building    → a run is in flight
 * - failed      → the last run errored
 * - seed-needed → songs-like slot with no seed artist liked yet
 * - pending     → never built / waiting for the next scheduled run
 */
@Composable
fun PlaylistPlaceholderCard(
    label: String,
    variant: String,
    modifier: Modifier = Modifier,
) {
    Column(modifier = modifier.width(176.dp).padding(horizontal = 8.dp)) {
        Box(
            modifier = Modifier
                .size(144.dp)
                .clip(RoundedCornerShape(FabledSwordFlatTokens.radiusSm))
                .background(MaterialTheme.colorScheme.surfaceVariant),
            contentAlignment = Alignment.Center,
        ) {
            Icon(
                imageVector = iconFor(variant),
                contentDescription = null,
                tint = MaterialTheme.colorScheme.onSurfaceVariant,
                modifier = Modifier.size(40.dp),
            )
            Surface(
                modifier = Modifier
                    .align(Alignment.BottomStart)
                    .padding(6.dp),
                shape = RoundedCornerShape(FabledSwordFlatTokens.radiusSm),
                color = MaterialTheme.colorScheme.surface.copy(alpha = STATUS_BG_ALPHA),
            ) {
                Text(
                    text = statusLabelFor(variant),
                    modifier = Modifier.padding(horizontal = 8.dp, vertical = 2.dp),
                    style = MaterialTheme.typography.labelSmall,
                    color = MaterialTheme.colorScheme.onSurface,
                )
            }
        }
        Spacer(Modifier.height(8.dp))
        Text(
            text = label,
            style = MaterialTheme.typography.titleSmall,
            color = MaterialTheme.colorScheme.onSurface,
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
        )
        Text(
            text = subtitleFor(variant),
            style = MaterialTheme.typography.bodySmall,
            color = MaterialTheme.colorScheme.onSurfaceVariant,
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
        )
    }
}

private const val STATUS_BG_ALPHA = 0.85f

private fun iconFor(variant: String) = when (variant) {
    "failed" -> Lucide.TriangleAlert
    "building" -> Lucide.RefreshCw
    else -> Lucide.ListMusic
}

private fun statusLabelFor(variant: String): String = when (variant) {
    "building" -> "Building"
    "failed" -> "Failed"
    "seed-needed" -> "Needs a seed"
    else -> "Pending"
}

private fun subtitleFor(variant: String): String = when (variant) {
    "building" -> "Generating now…"
    "failed" -> "Last build failed"
    "seed-needed" -> "Like an artist to seed this"
    else -> "Coming soon"
}
