// ProfileViewModel.kt
package com.example.initiateaphonecallapp.ui.profile

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.initiateaphonecallapp.data.model.ApigetInfoResponse
import com.example.initiateaphonecallapp.data.model.Profile
import com.example.initiateaphonecallapp.data.network.RetrofitClient
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

sealed class ProfileState {
    object Loading : ProfileState()
    data class Success(val profile: Profile) : ProfileState()
    data class Error(val message: String) : ProfileState()
}

class ProfileViewModel : ViewModel() {

    private val _profileState = MutableStateFlow<ProfileState>(ProfileState.Loading)
    val profileState: StateFlow<ProfileState> = _profileState.asStateFlow()

    init {
        loadProfile()
    }

    fun loadProfile() {
        viewModelScope.launch {
            _profileState.value = ProfileState.Loading
            try {
                val response = RetrofitClient.loginApiService.getInfo()
                if (response.isSuccessful) {
                    val apiResponse = response.body()
                    if (apiResponse != null && apiResponse.code == 200) {
                        val user = apiResponse.user
                        val profile = Profile(
                            userId = user.userId,
                            userName = user.userName,
                            nickName = user.nickName,
                            email = user.email ?: "未设置",
                            phonenumber = user.phonenumber ?: "未设置",
                            sex = when (user.sex) {
                                "0" -> "男"
                                "1" -> "女"
                                else -> "未知"
                            },
                            avatar = user.avatar,
                            deptName = user.dept.deptName,
                            roleNames = user.roles.map { it.roleName },
                            createTime = user.createTime ?: "未知",
                            loginIp = user.loginIp ?: "未知",
                            loginDate = user.loginDate ?: "未知",
                            status = when (user.status) {
                                "0" -> "正常"
                                "1" -> "停用"
                                else -> "未知"
                            }
                        )
                        _profileState.value = ProfileState.Success(profile)
                    } else {
                        _profileState.value = ProfileState.Error(
                            apiResponse?.msg ?: "获取个人信息失败"
                        )
                    }
                } else {
                    _profileState.value = ProfileState.Error("网络请求失败: ${response.code()}")
                }
            } catch (e: Exception) {
                _profileState.value = ProfileState.Error("加载失败: ${e.message}")
            }
        }
    }

    fun refreshProfile() {
        loadProfile()
    }
}