package com.rosetta.messenger.domain.chats.usecase

import com.rosetta.messenger.crypto.CryptoManager
import com.rosetta.messenger.network.TransportManager
import javax.inject.Inject

data class EncryptAndUploadAttachmentCommand(
    val payload: String,
    val attachmentPassword: String,
    val attachmentId: String,
    val isSavedMessages: Boolean
)

data class EncryptAndUploadAttachmentResult(
    val encryptedBlob: String,
    val transportTag: String,
    val transportServer: String
)

class EncryptAndUploadAttachmentUseCase @Inject constructor() {
    suspend operator fun invoke(command: EncryptAndUploadAttachmentCommand): EncryptAndUploadAttachmentResult {
        val encryptedBlob = CryptoManager.encryptWithPassword(command.payload, command.attachmentPassword)
        if (command.isSavedMessages) {
            return EncryptAndUploadAttachmentResult(
                encryptedBlob = encryptedBlob,
                transportTag = "",
                transportServer = ""
            )
        }

        val uploadTag = TransportManager.uploadFile(command.attachmentId, encryptedBlob)
        val transportServer =
            if (uploadTag.isNotEmpty()) {
                TransportManager.getTransportServer().orEmpty()
            } else {
                ""
            }

        return EncryptAndUploadAttachmentResult(
            encryptedBlob = encryptedBlob,
            transportTag = uploadTag,
            transportServer = transportServer
        )
    }
}
