package com.aiosman.ravenow.entity

import com.aiosman.ravenow.data.ListContainer
import com.aiosman.ravenow.data.ServiceException
import com.aiosman.ravenow.data.api.ApiClient
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

/**
 * 群聊房间
 */

data class RoomEntity(
    val id: Int,
    val name: String,
    val description: String,
    val trtcRoomId: String,
    val trtcType: String,
    val cover: String,
    val avatar: String,
    val recommendBanner: String,
    val isRecommended: Boolean,
    val allowInHot: Boolean,
    val creator: CreatorEntity,
    val userCount: Int,
    val maxMemberLimit: Int,
    val canJoin: Boolean,
    val canJoinCode: Int,
    val users: List<UsersEntity>,
)

data class CreatorEntity(
    val id: Int,
    val userId: String,
    val trtcUserId: String,
    val profile: ProfileEntity
)

data class UsersEntity(
    val id: Int,
    val userId: String,
    val profile: ProfileEntity
)

data class ProfileEntity(
    val id: Int,
    val username: String,
    val nickname: String,
    val avatar: String,
    val banner: String,
    val bio: String,
    val trtcUserId: String,
    val chatAIId: String,
    val aiAccount: Boolean,
)

class RoomLoader : DataLoader<AgentEntity,AgentLoaderExtraArgs>() {
    override suspend fun fetchData(
        page: Int,
        pageSize: Int,
        extra: AgentLoaderExtraArgs
    ): ListContainer<AgentEntity> {
        val result = ApiClient.api.getAgent(
            page = page,
            pageSize = pageSize,

        )
        val data =  result.body()?.let {
            ListContainer(
                list = it.data.list.map { it.toAgentEntity()},
                total = it.data.total,
                page = page,
                pageSize = pageSize
            )
        }
        if (data == null) {
            throw ServiceException("Failed to get agent")
        }
        return data
    }


}