package com.fabledsword.minstrel.admin.data

import com.fabledsword.minstrel.api.endpoints.AdminQuarantineApi
import com.fabledsword.minstrel.models.AdminQuarantineItemRef
import com.fabledsword.minstrel.models.AdminQuarantineReportRef
import com.fabledsword.minstrel.models.wire.AdminQuarantineItemWire
import com.fabledsword.minstrel.models.wire.AdminQuarantineReportWire
import retrofit2.Retrofit
import retrofit2.create
import javax.inject.Inject
import javax.inject.Singleton

/**
 * Read-through accessor for the admin quarantine queue. Mirrors the
 * Flutter `AdminQuarantineController`. No Room cache — same rationale
 * as AdminRequests. Three resolution actions all delete the row from
 * the queue once the server accepts them.
 */
@Singleton
class AdminQuarantineRepository @Inject constructor(
    retrofit: Retrofit,
) {
    private val api: AdminQuarantineApi = retrofit.create()

    suspend fun list(): List<AdminQuarantineItemRef> = api.list().map { it.toDomain() }

    suspend fun resolve(trackId: String) = api.resolve(trackId)

    suspend fun deleteFile(trackId: String) = api.deleteFile(trackId)

    suspend fun deleteViaLidarr(trackId: String) = api.deleteViaLidarr(trackId)
}

// ── Mappers ──

private fun AdminQuarantineReportWire.toDomain(): AdminQuarantineReportRef =
    AdminQuarantineReportRef(
        userId = userId,
        username = username,
        reason = reason,
        notes = notes,
        createdAt = createdAt,
    )

private fun AdminQuarantineItemWire.toDomain(): AdminQuarantineItemRef =
    AdminQuarantineItemRef(
        trackId = trackId,
        trackTitle = trackTitle,
        artistName = artistName,
        albumTitle = albumTitle,
        albumId = albumId,
        lidarrAlbumMbid = lidarrAlbumMbid,
        reportCount = reportCount,
        latestAt = latestAt,
        reasonCounts = reasonCounts,
        reports = reports.map { it.toDomain() },
    )
