package dev.kaizen.app.db

import dev.kaizen.app.net.ConversationSummary
import dev.kaizen.app.net.StoredMessage

fun ConversationSummary.toEntity() = ConversationEntity(
    id = id,
    title = title,
    locked = locked,
    pinned = pinned,
    updatedAt = updatedAt,
    cachedAt = System.currentTimeMillis(),
    titleGenerated = titleGenerated,
)

fun ConversationEntity.toSummary() = ConversationSummary(
    id = id,
    title = title,
    locked = locked,
    pinned = pinned,
    updatedAt = updatedAt,
    titleGenerated = titleGenerated,
)

fun StoredMessage.toEntity(conversationId: String, sortOrder: Int) = MessageEntity(
    id = id,
    conversationId = conversationId,
    role = role,
    content = content,
    attachments = attachments,
    sortOrder = sortOrder,
    reasoning = reasoning,
    sources = sources ?: emptyList(),
)

fun MessageEntity.toStoredMessage() = StoredMessage(
    id = id,
    role = role,
    content = content,
    attachments = attachments,
    reasoning = reasoning,
    sources = sources.takeIf { it.isNotEmpty() },
)
