package com.aletheia.app.model

data class AudioBook(
    val id: Long = 0,
    val sourceBookId: Long,
    val title: String,
    val author: String,
    val filePath: String = "",
    val remoteJobId: String? = null,
    val status: Status = Status.QUEUED,
    val processedCharacters: Long = 0,
    val totalCharacters: Long = 0,
    val durationMs: Long = 0,
    val positionMs: Long = 0,
    val chaptersJson: String = "[]",
    val currentChapter: String? = null,
    val errorMessage: String? = null,
    val createdAt: Long = System.currentTimeMillis(),
    val updatedAt: Long = System.currentTimeMillis()
) {
    val progressPercent: Int
        get() = if (totalCharacters <= 0) 0 else
            ((processedCharacters * 100L) / totalCharacters).toInt().coerceIn(0, 100)

    enum class Status(val databaseValue: String) {
        QUEUED("queued"), GENERATING("generating"), READY("ready"),
        FAILED("failed"), CANCELLED("cancelled");

        companion object {
            fun fromDatabase(value: String?): Status =
                entries.firstOrNull { it.databaseValue == value } ?: FAILED
        }
    }
}
