package com.fabledsword.minstrel.api.endpoints

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
import retrofit2.http.Body
import retrofit2.http.POST

/**
 * Retrofit interface for device diagnostics ingest (M9). One call
 * uploads a batch of buffered events. The server stores them only when
 * the account's debug_mode_enabled flag is on (it returns 204 otherwise,
 * which the uploader treats as success so it can drop the batch).
 */
interface DiagnosticsApi {
    @POST("api/diagnostics")
    suspend fun report(@Body body: DiagnosticsReportRequest)
}

@Serializable
data class DiagnosticsReportRequest(
    @SerialName("client_id") val clientId: String,
    @SerialName("app_version") val appVersion: String? = null,
    @SerialName("os_version") val osVersion: String? = null,
    val events: List<DiagnosticEventWire>,
)

@Serializable
data class DiagnosticEventWire(
    val kind: String,
    // Device-clock epoch milliseconds. The server stamps its own
    // received_at; both are stored so a skewed device clock is visible.
    @SerialName("occurred_at") val occurredAt: Long,
    // Opaque structured payload carrying the event sub-type + fields.
    val payload: JsonElement,
)
