package com.aiosman.ravenow.ui.group

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.aiosman.ravenow.AppStore
import com.aiosman.ravenow.data.AccountService
import com.aiosman.ravenow.data.AccountServiceImpl
import com.aiosman.ravenow.data.api.ApiClient
import kotlinx.coroutines.launch

class AiAgentListViewModel : ViewModel() {
    private val accountService: AccountService = AccountServiceImpl()
    
    // 状态管理
    var aiAgents by mutableStateOf<List<GroupMember>>(emptyList())
        private set
    
    var isLoading by mutableStateOf(false)
        private set
    
    var isRefreshing by mutableStateOf(false)
        private set
    
    var isLoadingMore by mutableStateOf(false)
        private set
    
    var currentPage by mutableStateOf(1)
        private set
    
    var hasMoreData by mutableStateOf(true)
        private set
    
    var errorMessage by mutableStateOf<String?>(null)
        private set
    
    private val pageSize = 20
    
    init {
        loadAgents(1)
    }
    
    // 加载AI智能体数据
    fun loadAgents(page: Int, isRefresh: Boolean = false) {
        viewModelScope.launch {
            try {
                if (isRefresh) {
                    isRefreshing = true
                } else if (page == 1) {
                    isLoading = true
                } else {
                    isLoadingMore = true
                }
                
                errorMessage = null
                
                val response = accountService.getAgent(page, pageSize)
                if (response.isSuccessful && response.body() != null) {
                    val agentData = response.body()!!
                    val newAgents: List<GroupMember> = agentData.data.list.map { agent ->
                        GroupMember(
                            id = agent.openId,
                            name = agent.title,
                            avatar = "${ApiClient.BASE_API_URL+"/outside"}${agent.avatar}"+"?token="+"${AppStore.token}",
                            isAi = true
                        )
                    }
                    
                    if (isRefresh || page == 1) {
                        aiAgents = newAgents
                        currentPage = 1
                    } else {
                        aiAgents = aiAgents + newAgents
                        currentPage = page
                    }
                    
                    hasMoreData = newAgents.size >= pageSize
                } else {
                    errorMessage = "获取AI智能体列表失败: ${response.message()}"
                }
            } catch (e: Exception) {
                errorMessage = "获取AI智能体列表失败: ${e.message}"
            } finally {
                isLoading = false
                isRefreshing = false
                isLoadingMore = false
            }
        }
    }
    
    // 刷新数据
    fun refresh() {
        loadAgents(1, true)
    }
    
    // 加载更多数据
    fun loadMore() {
        if (hasMoreData && !isLoadingMore && !isLoading) {
            loadAgents(currentPage + 1)
        }
    }
    
    // 清除错误信息
    fun clearError() {
        errorMessage = null
    }
    
    // 搜索过滤
    fun getFilteredAgents(searchText: String): List<GroupMember> {
        return if (searchText.isEmpty()) {
            aiAgents
        } else {
            aiAgents.filter { it.name.contains(searchText, ignoreCase = true) }
        }
    }
}
