package com.aiosman.ravenow.ui.index.tabs.moment.tabs.recommend

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aiosman.ravenow.LocalAppTheme
import com.aiosman.ravenow.LocalNavController
import com.aiosman.ravenow.R
import com.aiosman.ravenow.data.UserService
import com.aiosman.ravenow.data.UserServiceImpl
import com.aiosman.ravenow.ui.NavigationRoute
import com.aiosman.ravenow.ui.composables.CustomAsyncImage
import com.aiosman.ravenow.ui.navigateToChatAi
import kotlinx.coroutines.launch

/**
 * Prompt推荐Item组件
 * 精美的AI Agent卡片样式
 */
@Composable
fun PromptRecommendationItem(
    id: Long,
    title: String,
    desc: String,
    avatar: String?,
    openId: String,
    modifier: Modifier = Modifier
) {
    val AppColors = LocalAppTheme.current
    val navController = LocalNavController.current
    val scope = rememberCoroutineScope()
    val userService: UserService = UserServiceImpl()
    
    // 导航到个人资料
    fun navigateToProfile() {
        scope.launch {
            try {
                val profile = userService.getUserProfileByOpenId(openId)
                // Prompt推荐的一定是AI账号，直接传递isAiAccount = true
                navController.navigate(
                    NavigationRoute.AccountProfile.route
                        .replace("{id}", profile.id.toString())
                        .replace("{isAiAccount}", "true")
                )
            } catch (e: Exception) {
                // 处理错误，避免崩溃
                e.printStackTrace()
            }
        }
    }
    
    // 导航到AI聊天
    fun navigateToChatAi() {
        scope.launch {
            try {
                val profile = userService.getUserProfileByOpenId(openId)
                navController.navigateToChatAi(profile.id.toString())
            } catch (e: Exception) {
                // 处理错误，避免崩溃
                e.printStackTrace()
            }
        }
    }
    
    Box(
        modifier = modifier
            .fillMaxSize()
            .background(Color.Black)
            .clickable(
                onClick = { navigateToProfile() }
            )
    ) {
        // 背景大图
        CustomAsyncImage(
            imageUrl = avatar,
            contentDescription = "AI Agent 头像",
            modifier = Modifier.fillMaxSize(),
            contentScale = ContentScale.Crop,
            defaultRes = R.drawable.default_avatar
        )
        
        // 渐变遮罩（底部深色）
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(
                    Brush.verticalGradient(
                        colors = listOf(
                            Color.Transparent,
                            Color.Black.copy(alpha = 0.7f)
                        ),
                        startY = 200f
                    )
                )
        )
        
        // 内容区域
        Box(
            modifier = Modifier
                .fillMaxSize(),
            contentAlignment = Alignment.BottomStart
        ) {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(start = 24.dp, end = 24.dp, bottom = 40.dp)
            ) {
                // AI Agent 标签
                Surface(
                    modifier = Modifier.padding(bottom = 16.dp),
                    shape = RoundedCornerShape(8.dp),
                    color = Color.White.copy(alpha = 0.2f)
                ) {
                    Text(
                        text = "✨ AI Agent",
                        modifier = Modifier.padding(horizontal = 12.dp, vertical = 6.dp),
                        fontSize = 12.sp,
                        color = Color.White,
                        fontWeight = FontWeight.Medium
                    )
                }
                
                // 名称
                Text(
                    text = title,
                    fontSize = 32.sp,
                    fontWeight = FontWeight.Bold,
                    color = Color.White,
                    modifier = Modifier.padding(bottom = 12.dp)
                )
                
                // 描述
                Text(
                    text = desc,
                    fontSize = 16.sp,
                    color = Color.White.copy(alpha = 0.9f),
                    lineHeight = 22.sp,
                    maxLines = 3,
                    overflow = TextOverflow.Ellipsis,
                    modifier = Modifier.padding(bottom = 24.dp)
                )
                
                // Start chatting 按钮
                Button(
                    onClick = {
                        // 导航到AI聊天页面（区分AI聊天和普通聊天）
                        navigateToChatAi()
                    },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 16.dp),
                    shape = RoundedCornerShape(24.dp),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = Color.White,
                        contentColor = Color.Black
                    )
                ) {
                    Text(
                        text = "💬 Start chatting",
                        fontSize = 16.sp,
                        fontWeight = FontWeight.Bold,
                        modifier = Modifier.padding(vertical = 8.dp)
                    )
                }
            }
        }
    }
}
