package com.aiosman.ravenow.data

import com.aiosman.ravenow.data.api.ApiClient
import com.aiosman.ravenow.entity.AccountProfileEntity

data class UserAuth(
    val id: Int,
    val token: String? = null,
    val isGuest: Boolean = false
)

/**
 * 用户相关 Service
 */
interface UserService {
    /**
     * 获取用户信息
     * @param id 用户ID
     * @return 用户信息
     */
    suspend fun getUserProfile(id: String): AccountProfileEntity

    /**
     * 关注用户
     * @param id 用户ID
     */
    suspend fun followUser(id: String)

    /**
     * 取消关注用户
     * @param id 用户ID
     */
    suspend fun unFollowUser(id: String)

    /**
     * 获取用户列表
     * @param pageSize 分页大小
     * @param page 页码
     * @param nickname 昵称搜索
     * @param followerId 粉丝ID,账号粉丝
     * @param followingId 关注ID,账号关注
     * @return 用户列表
     */
    suspend fun getUsers(
        pageSize: Int = 20,
        page: Int = 1,
        nickname: String? = null,
        followerId: Int? = null,
        followingId: Int? = null
    ): ListContainer<AccountProfileEntity>


    /**
     * 获取用户信息
     * @param id 用户ID
     * @return 用户信息
     */

    suspend fun getUserProfileByTrtcUserId(id: String,includeAI: Int):AccountProfileEntity

    /**
     * 获取用户信息
     * @param id 用户ID
     * @return 用户信息
     */

    suspend fun getUserProfileByOpenId(id: String):AccountProfileEntity

}

class UserServiceImpl : UserService {
    override suspend fun getUserProfile(id: String): AccountProfileEntity {
        val resp = ApiClient.api.getAccountProfileById(id.toInt())
        val body = resp.body() ?: throw ServiceException("Failed to get account")
        return body.data.toAccountProfileEntity()
    }

    override suspend fun followUser(id: String) {
        val resp = ApiClient.api.followUser(id.toInt())
        return
    }

    override suspend fun unFollowUser(id: String) {
        val resp = ApiClient.api.unfollowUser(id.toInt())
        return
    }

    override suspend fun getUsers(
        pageSize: Int,
        page: Int,
        nickname: String?,
        followerId: Int?,
        followingId: Int?
    ): ListContainer<AccountProfileEntity> {
        val resp = ApiClient.api.getUsers(
            page = page,
            pageSize = pageSize,
            search = nickname,
            followerId = followerId,
            followingId = followingId
        )
        val body = resp.body() ?: throw ServiceException("Failed to get account")
        return ListContainer<AccountProfileEntity>(
            list = body.list.map { it.toAccountProfileEntity() },
            page = body.page,
            total = body.total,
            pageSize = body.pageSize,
        )
    }

    override suspend fun getUserProfileByTrtcUserId(id: String,includeAI: Int): AccountProfileEntity {
        val resp = ApiClient.api.getAccountProfileByTrtcUserId(id,includeAI)
        val body = resp.body() ?: throw ServiceException("Failed to get account")
        return body.data.toAccountProfileEntity()
    }

    override suspend fun getUserProfileByOpenId(id: String): AccountProfileEntity {
        val resp = ApiClient.api.getAccountProfileByOpenId(id)
        val body = resp.body() ?: throw ServiceException("Failed to get account")
        return body.data.toAccountProfileEntity()
    }
}