package ui


import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items  // 반드시 수동 import 확인
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*

import io.ktor.client.engine.cio.CIO
// 아래 두 import가 'delegate' 에러를 해결합니다.
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.coroutines.launch
import model.AppConfig
import model.BalanceSummary
import model.RankingStock
import model.RealTimeTrade
import model.StockHolding
import model.TradeType
import network.KisTradeService
import util.MarketUtil

@Composable
fun TradeLogRow(trade: RealTimeTrade) {
    val color = when (trade.type) {
        TradeType.BUY -> Color(0xFFE03E2D)
        TradeType.SELL -> Color(0xFF0E62CF)
        else -> Color.DarkGray
    }

    // 대량 체결(예: 1000주 이상) 시 연한 배경색 강조
    val isLargeTrade = (trade.volume.replace(",", "").toIntOrNull() ?: 0) >= 1000
    val rowBgColor = if (isLargeTrade) color.copy(alpha = 0.05f) else Color.Transparent

    Row(
        modifier = Modifier.fillMaxWidth().background(rowBgColor).padding(vertical = 6.dp, horizontal = 4.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(trade.time, modifier = Modifier.weight(1f), fontSize = 12.sp, color = Color.Gray, textAlign = TextAlign.Center)
        Text(trade.price, modifier = Modifier.weight(1.5f), fontSize = 12.sp, fontWeight = FontWeight.Bold, color = color, textAlign = TextAlign.End)
        Text(trade.change, modifier = Modifier.weight(1f), fontSize = 11.sp, color = color, textAlign = TextAlign.End)
        Text(
            text = trade.volume,
            modifier = Modifier.weight(1f),
            fontSize = 12.sp,
            fontWeight = if (isLargeTrade) FontWeight.ExtraBold else FontWeight.Normal,
            color = color,
            textAlign = TextAlign.End
        )
    }
}