// COMPILE-TIME STUB — see ../MapMap.kt header. `-PskipMapmap` builds only.
package ai.mapmap.territory

import ai.mapmap.stubOnly
import kotlinx.coroutines.flow.StateFlow
import java.io.File

/** Installed-territory record surfaced by TerritoryStore.territories. */
data class TerritoryInfo(
  val id: String,
  val name: String,
  val version: String,
  val sizeOnDiskBytes: Long,
  val active: Boolean,
)

/** Differential update plan returned by checkForUpdate(). */
class PlannedUpdate(
  val manifestJson: ByteArray,
  val manifestSig: ByteArray,
  val fromVersion: String?,
  val toVersion: String,
  val totalDownloadBytes: Long,
)

/**
 * App-supplied fetcher: the store hands back the paths it needs (channel
 * index-relative or absolute); the app performs the transport (with its own
 * auth) and returns the bytes. The core verifies everything it receives.
 */
fun interface LayerFetcher {
  fun fetch(path: String): ByteArray
}

/**
 * Coroutine wrapper over the core's territory manager. Constructor shape per
 * docs/sdks (0.1.0): rootDir + baked-in ed25519 verifying key hex.
 * checkForUpdate/applyUpdate shapes per docs/territories:
 *   check_for_update(id, index_json, index_sig, fetcher) → update?
 *   apply_update(id, update.manifest_json, update.manifest_sig, fetcher)
 * remove() is ASSUMED (no public removal API documented).
 */
class TerritoryStore(
  val rootDir: File,
  val verifyingKeyHex: String,
) {
  val territories: StateFlow<List<TerritoryInfo>>
    get() = stubOnly()

  suspend fun checkForUpdate(
    territoryId: String,
    indexJson: ByteArray,
    indexSignature: ByteArray,
    fetcher: LayerFetcher,
  ): PlannedUpdate? = stubOnly()

  suspend fun applyUpdate(
    territoryId: String,
    manifestJson: ByteArray,
    manifestSignature: ByteArray,
    fetcher: LayerFetcher,
  ) {
    stubOnly()
  }

  suspend fun remove(territoryId: String) {
    stubOnly()
  }
}
