package dev.inmo.postssystem.features.files.common

import dev.inmo.micro_utils.common.*
import dev.inmo.micro_utils.mime_types.MimeType
import dev.inmo.micro_utils.serialization.typed_serializer.TypedSerializer
import dev.inmo.postssystem.features.common.common.SimpleInputProvider
import dev.inmo.postssystem.features.common.common.SimpleInputProviderSerializer
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable

@Serializable(FileInfoSerializer::class)
sealed interface FileInfo {
    val name: FileName
    val mimeType: MimeType

    companion object {
        fun serializer(): KSerializer<FileInfo> = FileInfoSerializer
    }
}

object FileInfoSerializer : KSerializer<FileInfo> by TypedSerializer(
    "meta" to MetaFileInfo.serializer(),
    "full" to FullFileInfo.serializer(),
)

@Serializable
data class MetaFileInfo(override val name: FileName, override val mimeType: MimeType) : FileInfo

@Serializable
data class FullFileInfo(
    override val name: FileName,
    override val mimeType: MimeType,
    @Serializable(SimpleInputProviderSerializer::class)
    val inputProvider: SimpleInputProvider
) : FileInfo

fun FullFileInfo.toMetaFileInfo() = MetaFileInfo(name, mimeType)
