package com.aiwazian.messenger.services

import com.aiwazian.messenger.api.RetrofitInstance
import com.aiwazian.messenger.data.GroupInfo
import com.aiwazian.messenger.data.CreateGroupRequest
import com.aiwazian.messenger.data.UserInfo
import javax.inject.Inject

class GroupService @Inject constructor() {
    
    suspend fun create(groupInfo: GroupInfo): Long? {
        val response = RetrofitInstance.api.createGroup(
            CreateGroupRequest(groupInfo.name, groupInfo.bio.ifBlank { null })
        )
        return response.body()?.id
    }
    
    suspend fun get(id: Long): GroupInfo? {
        val response = RetrofitInstance.api.getGroup(id)
        return response.body()
    }
    
    suspend fun delete(id: Long): Boolean {
        val response = RetrofitInstance.api.deleteGroup(id)
        return response.isSuccessful
    }
    
    suspend fun getMembers(id: Long): List<UserInfo>? {
        val response = RetrofitInstance.api.getGroupMembers(id)
        return response.body()
    }
    
    suspend fun inviteUserToGroup(
        groupId: Long,
        userId: Long
    ): Boolean {
        return try {
            val response = RetrofitInstance.api.inviteUserToGroup(groupId, userId)
            response.isSuccessful
        } catch (e: Exception) {
            false
        }
    }

    suspend fun removeUserFromGroup(
        groupId: Long,
        userId: Long
    ): Boolean {
        return try {
            val response = RetrofitInstance.api.removeUserFromGroup(groupId, userId)
            response.isSuccessful
        } catch (e: Exception) {
            false
        }
    }
}
