package com.keylightpiano.pianotimeline.domain

import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Locale

/**
 * The clean, app-facing model. Real types, sane defaults, computed helpers.
 * The UI only ever touches this -- never the raw DTO.
 */
data class Recording(
    val id: String,
    val date: LocalDate,
    val artist: String,
    val title: String,
    val extraInfo: String,
    val performanceNumber: Int,
    val totalPerformances: Int,
    val fileName: String,
    val midiFileName: String?,
    val durationSeconds: Double,
    val soundStartSeconds: Double,
    val fileSize: Long,
    val isFavorite: Boolean = false,
    val isNew: Boolean = false,
    val favoritedAt: Long = 0L,
    // Computed live from the ExoPlayer disk cache (not persisted). True when the
    // full audio file is cached and therefore playable with no network.
    val isCached: Boolean = false
) {
    val hasMidi: Boolean get() = midiFileName != null

    /** "22 Jun 2026" style for display, in the device's current locale. */
    val formattedDate: String
        get() = date.format(
            DateTimeFormatter.ofPattern("d MMM yyyy", Locale.getDefault())
        )

    val groupingArtist: String
        get() = if (artist.contains("soundtrack", ignoreCase = true)) "Soundtracks" else artist

    val formattedDuration: String
        get() {
            val total = durationSeconds.toInt()
            val minutes = total / 60
            val seconds = total % 60
            return "%d:%02d".format(minutes, seconds)
        }

    val performanceLabel: String
        get() = "$performanceNumber / $totalPerformances"
}
