package com.fabledsword.minstrel.admin.data

import com.fabledsword.minstrel.api.endpoints.AdminInvitesApi
import com.fabledsword.minstrel.api.endpoints.CreateInviteBody
import com.fabledsword.minstrel.models.Invite
import com.fabledsword.minstrel.models.wire.InviteWire
import retrofit2.Retrofit
import retrofit2.create
import javax.inject.Inject
import javax.inject.Singleton

/**
 * Thin facade over `/api/admin/invites`. Same shape as the other
 * admin repos — direct REST, no caching.
 */
@Singleton
class AdminInvitesRepository @Inject constructor(retrofit: Retrofit) {
    private val api: AdminInvitesApi = retrofit.create()

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

    suspend fun create(note: String?): Invite =
        api.create(CreateInviteBody(note = note?.takeIf { it.isNotEmpty() })).toDomain()

    suspend fun revoke(token: String) {
        api.revoke(token)
    }
}

private fun InviteWire.toDomain(): Invite = Invite(
    token = token,
    invitedBy = invitedBy,
    note = note,
    createdAt = createdAt,
    expiresAt = expiresAt,
    redeemedAt = redeemedAt,
    redeemedBy = redeemedBy,
)
