package com.aiosman.ravenow.ui.profile

import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.lifecycle.viewmodel.compose.viewModel
import com.aiosman.ravenow.LocalNavController
import com.aiosman.ravenow.exp.viewModelFactory
import com.aiosman.ravenow.ui.index.tabs.profile.MyProfileViewModel
import com.aiosman.ravenow.ui.index.tabs.profile.ProfileV3
import com.aiosman.ravenow.ui.navigateToChat
import com.aiosman.ravenow.ui.navigateToPost

@Composable
fun AccountProfileV2(id: String, isAiAccount: Boolean = false){
    val model: AccountProfileViewModel = viewModel(factory = viewModelFactory {
        AccountProfileViewModel()
    }, key = "viewModel_${id}")
    val navController = LocalNavController.current
    LaunchedEffect(Unit) {
        model.loadProfile(id)
        MyProfileViewModel.loadProfile()
    }
    var isSelf = false
    if (id == MyProfileViewModel.profile?.id.toString()) {
        isSelf = true
    }
    ProfileV3(
        moments = model.moments,
        agents = model.agents,
        profile = model.profile,
        isSelf = isSelf,
        isAiAccount = isAiAccount, // 从参数传入AI账户判断
        postCount = model.momentLoader.total,
        onLoadMore = {
            Log.d("AccountProfileV2", "onLoadMore被调用")
            model.loadMoreMoment()
        },
        onLike = { moment ->
            // TODO: 实现点赞逻辑
        },
        onComment = { moment ->
            navController.navigateToPost(moment.id)
        },
        onChatClick = {
            model.profile?.let {
                navController.navigateToChat(it.id.toString())
            }
        },
        onFollowClick = {
            model.profile?.let {
                if (it.isFollowing) {
                    model.unFollowUser(id)
                } else {
                    model.followUser(id)
                }
            }
        },
        onAgentClick = { agent ->
            // TODO: 处理Agent点击事件，导航到聊天页面
        }
    )
}