package com.fabledsword.minstrel.cache.db.entities

import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant

/**
 * One row per pending offline-write. Mirrors
 * `flutter_client/lib/cache/db.dart`'s `CachedMutations` Drift table.
 *
 * MutationQueue.enqueue() inserts a row when a server-write fails with
 * an IOException; MutationReplayer.drain() pops and re-attempts each
 * row when connectivity returns (Phase 12.2).
 *
 * `kind` is a stable string registered in `MutationKind` — the replayer
 * looks up the handler in a kind→suspend-function map. Unknown kinds
 * are dropped so a malformed row can't wedge the queue forever.
 *
 * `payload` is the JSON-serialized args needed to replay the REST call.
 * Shape is kind-specific; the kind enum doubles as a tag for which
 * decoder to apply at drain time.
 */
@Entity(tableName = "cached_mutations")
data class CachedMutationEntity(
    @PrimaryKey(autoGenerate = true) val id: Long = 0,
    val kind: String,
    val payload: String,
    val createdAt: Instant = Clock.System.now(),
    val lastAttemptAt: Instant? = null,
    val attempts: Int = 0,
)
