@file:Suppress("HardCodedStringLiteral")

package eu.gaudian.translator.view.composable

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

/**
 * Centralized model icon resolver. Heuristics based on model name and provider.
 * This is the single place to adjust mappings, keeping UI components simple.
 */
object ModelIconResolver {
    sealed interface BadgeSpec {
        data class IconSpec(val icon: ImageVector, val tint: Color? = null) : BadgeSpec
        data class EmojiSpec(val emoji: String, val tint: Color? = null) : BadgeSpec
    }

    fun resolveAll(modelName: String, providerKey: String?): List<BadgeSpec> {
        val name = modelName.lowercase() + providerKey?.lowercase()
        val provider = providerKey?.lowercase()


        val specs = mutableListOf<BadgeSpec>()

        // Priority 1: Special keywords (can add multiple)
        if (name.contains("free")) {
            // Use money emoji for "free" to showcase emoji support
            specs.add(BadgeSpec.EmojiSpec("💲", Color(0xFF2E7D32))) // green
        }
        if (name.contains("turbo") || name.contains("flash") || name.contains("fast")) {
            specs.add(BadgeSpec.IconSpec(AppIcons.Flash, Color(0xFFFFC107))) // yellow
        }
        if (name.contains("-mini") || name.contains("nano")  || name.contains("small") || name.contains(" mini")) {
            specs.add(BadgeSpec.EmojiSpec("🪶", Color.White))
        }
        if (name.contains("-large") || name.contains("pro")  || name.contains("large") || name.contains(" pro")) {
            specs.add(BadgeSpec.EmojiSpec("💎", Color.White))
        }
        if (name.contains("-vision") || name.contains("-image") || name.contains("video")) {
            specs.add(BadgeSpec.EmojiSpec("⛔️", Color.White))
            specs.add(BadgeSpec.EmojiSpec("👁️", Color.White))

        }
        if ( name.contains("-audio")) {
            specs.add(BadgeSpec.EmojiSpec("⛔️", Color.White))
            specs.add(BadgeSpec.EmojiSpec("🎧", Color.White))

        }

        @Suppress("UnusedVariable") val sizeIcon = when {
            Regex("(^|[^0-9])120b([^0-9]|$)").containsMatchIn(name) -> BadgeSpec.EmojiSpec("1️⃣2️⃣0️⃣", Color(0xFFF44336))
            Regex("(^|[^0-9])70b([^0-9]|$)").containsMatchIn(name) -> BadgeSpec.EmojiSpec("7️⃣0️⃣", Color(0xFFFF9800))
            Regex("(^|[^0-9])(?!1)20b([^0-9]|$)").containsMatchIn(name) -> BadgeSpec.EmojiSpec("2️⃣0️⃣", Color(0xFFFFC107))
            Regex("(^|[^0-9])8b(?!0)([^0-9]|$)").containsMatchIn(name) -> BadgeSpec.EmojiSpec("8️⃣", Color(0xFF43A047))
            Regex("(^|[^0-9])7b(?!0)([^0-9]|$)").containsMatchIn(name) -> BadgeSpec.EmojiSpec("7️⃣", Color(0xFF4CAF50))
            Regex("(^|[^0-9])1b(?!0)([^0-9]|$)").containsMatchIn(name) -> BadgeSpec.EmojiSpec("1️⃣", Color(0xFF64B5F6))
            else -> null
        }
        //TODO for now no size icon until it is fixed
        //if (sizeIcon != null) specs.add(sizeIcon)

        // Instruction-tuned
        if (name.contains("instruct")) {
            specs.add(BadgeSpec.IconSpec(AppIcons.Science, null))
        }

        // Family heuristics (mix icons and emojis to demonstrate capability)
        if (name.contains("llama") || name.contains("meta")) specs.add(BadgeSpec.EmojiSpec("🦙", null))
        if (name.contains("mistral") || name.contains("mixtral"))specs.add(BadgeSpec.EmojiSpec("🐱", null))
        if (name.contains("gpt") || provider == "openai") specs.add(BadgeSpec.EmojiSpec("🌀", null))
        if (name.contains("deepseek")) specs.add(BadgeSpec.EmojiSpec("🐋") )
        if (name.contains("gemini") || name.contains("google") ||name.contains("gemma") || provider == "gemini" || provider == "google") specs.add(BadgeSpec.EmojiSpec("👥") )
        if (name.contains("qwen")) specs.add(BadgeSpec.EmojiSpec("🌊") )
        if (name.contains("phi") || provider?.contains("microsoft") == true) specs.add(BadgeSpec.IconSpec(AppIcons.Science, null))
        if (name.contains("ibm") || name.contains("granite")) specs.add(BadgeSpec.IconSpec(AppIcons.Business, null))
        if (name.contains("moonshot") || name.contains("moonshotai") || name.contains("moon") ) specs.add(BadgeSpec.EmojiSpec("🌕") )

        return specs
    }

}

@Composable
fun ModelBadges(
    modelDisplayOrId: String,
    providerKey: String?,
    modifier: Modifier = Modifier,
    iconSizeDp: Int = 16,
    spacingDp: Int = 4,
) {
    val badges = ModelIconResolver.resolveAll(modelDisplayOrId, providerKey)
    if (badges.isEmpty()) return
    androidx.compose.foundation.layout.Row(modifier = modifier) {
        badges.forEachIndexed { index, b ->
            when (b) {
                is ModelIconResolver.BadgeSpec.IconSpec -> {
                    Icon(
                        imageVector = b.icon,
                        contentDescription = null,
                        tint = b.tint ?: MaterialTheme.colorScheme.primary,
                        modifier = Modifier.size(iconSizeDp.dp)
                    )
                }
                is ModelIconResolver.BadgeSpec.EmojiSpec -> {
                    // Center the emoji inside a fixed-size box and scale font to avoid clipping
                    Box(modifier = Modifier.size(iconSizeDp.dp), contentAlignment = Alignment.Center) {
                        val fs = (iconSizeDp * 0.88f).sp
                        Text(
                            text = b.emoji,
                            color = b.tint ?: MaterialTheme.colorScheme.primary,
                            fontWeight = FontWeight.Normal,
                            fontSize = fs,
                            lineHeight = fs,
                            maxLines = 1
                        )
                    }
                }
            }
            if (index != badges.lastIndex) {
                androidx.compose.foundation.layout.Spacer(Modifier.width(spacingDp.dp))
            }
        }
    }
}