package com.seven.ikar.kotlin.core.update

data class ArgusAppUpdateConfig(
    val baseUrl: String,
    val slug: String,
    val platform: String = "android",
    val channel: String = "stable"
)

data class InstalledIkarApp(
    val packageName: String,
    val versionCode: Long,
    val versionName: String?
)

data class ArgusPublishedRelease(
    val id: String,
    val version: String,
    val channel: String,
    val platform: String,
    val packageKind: String,
    val androidVersionCode: Long?,
    val downloadPath: String,
    val originalFileName: String,
    val contentType: String,
    val packageSizeBytes: Long,
    val sha256: String,
    val publishedAt: String,
    val notes: String?
)

data class ArgusAppManifest(
    val slug: String,
    val name: String,
    val summary: String,
    val description: String,
    val androidPackageName: String?,
    val repositoryUrl: String?,
    val homepageUrl: String?,
    val release: ArgusPublishedRelease
)

data class AvailableIkarUpdate(
    val config: ArgusAppUpdateConfig,
    val installedApp: InstalledIkarApp,
    val manifest: ArgusAppManifest
) {
    val downloadUrl: String
        get() = config.baseUrl.trimEnd('/') + manifest.release.downloadPath
}

sealed interface AppUpdateCheckResult {
    data class NotPublished(val reason: String) : AppUpdateCheckResult

    data class UpToDate(val manifest: ArgusAppManifest?) : AppUpdateCheckResult

    data class UpdateAvailable(val update: AvailableIkarUpdate) : AppUpdateCheckResult

    data class Incompatible(val reason: String) : AppUpdateCheckResult

    data class Failed(val reason: String, val cause: Throwable? = null) : AppUpdateCheckResult
}

sealed interface AppUpdateSubmissionResult {
    data class Submitted(val message: String) : AppUpdateSubmissionResult

    data class Rejected(val message: String) : AppUpdateSubmissionResult
}

data class AppUpdateInstallerEvent(
    val state: String,
    val packageName: String?,
    val releaseVersion: String?,
    val versionCode: Long?,
    val detail: String?,
    val updatedAtEpochMillis: Long
)

object AppUpdateInstallerEventState {
    const val Submitted = "submitted"
    const val ApprovalRequired = "approval_required"
    const val Installed = "installed"
    const val Failed = "failed"
}
