package com.fabledsword.minstrel.models

/**
 * Status of a Lidarr request. Stored on the wire as a plain string;
 * the typed enum keeps switch / when blocks exhaustive and lets the
 * UI map directly to a status pill color without raw-string matches.
 */
enum class RequestStatus {
    PENDING, APPROVED, REJECTED, COMPLETED, FAILED, UNKNOWN;

    companion object {
        fun fromWire(s: String): RequestStatus = when (s) {
            "pending" -> PENDING
            "approved" -> APPROVED
            "rejected" -> REJECTED
            "completed" -> COMPLETED
            "failed" -> FAILED
            else -> UNKNOWN
        }
    }

    val wire: String get() = name.lowercase()
}

/**
 * One Lidarr request the user has submitted. Mirrors
 * `flutter_client/lib/models/admin_request.dart AdminRequest` —
 * shared between the user-side `/api/requests` view and the admin
 * cross-user view since the wire shape is identical.
 *
 * `displayName` picks the right field based on kind:
 *   - album → albumTitle (fallback artistName)
 *   - track → trackTitle (fallback artistName)
 *   - artist → artistName
 *
 * `listenHrefId` returns the most-specific matched library ID once the
 * request completes (track → album → artist) so the "Listen" CTA can
 * navigate to the right detail screen. Returns null until the
 * server's ingest job populates the matched_* fields.
 */
data class RequestRef(
    val id: String,
    val userId: String,
    val status: RequestStatus,
    val kind: String,
    val artistName: String,
    val albumTitle: String? = null,
    val trackTitle: String? = null,
    val requestedAt: String,
    val decidedAt: String? = null,
    val notes: String? = null,
    val importedAlbumCount: Int = 0,
    val importedTrackCount: Int = 0,
    val matchedTrackId: String? = null,
    val matchedAlbumId: String? = null,
    val matchedArtistId: String? = null,
) {
    val displayName: String
        get() = when (kind) {
            "album" -> albumTitle ?: artistName
            "track" -> trackTitle ?: artistName
            else -> artistName
        }
}
