package eu.friedcutlet.yokan.services.backup import eu.friedcutlet.yokan.database.DbCategory import eu.friedcutlet.yokan.database.DbChangedPart import eu.friedcutlet.yokan.database.DbChapter import eu.friedcutlet.yokan.database.DbCustomButton import eu.friedcutlet.yokan.database.DbHistory import eu.friedcutlet.yokan.database.DbItemType import eu.friedcutlet.yokan.database.DbManga import eu.friedcutlet.yokan.database.DbSettings import eu.friedcutlet.yokan.database.DbSource import eu.friedcutlet.yokan.database.DbTrack import eu.friedcutlet.yokan.database.DbTrackPreference import eu.friedcutlet.yokan.database.DbUpdate import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json /** * Output container for a backup. * - [Yokan]: native `.json` / `.json.gz` (gzipped JSON). * - [Mangayomi]: a `.backup` ZIP holding one `*.backup.db` plain-JSON entry, * in the schema the old Flutter Mangayomi app reads (version "2", singular * keys, enum-as-index). Lets the old app restore Yokan backups. * - [Tachiyomi]: an Animetail `Backup` protobuf Mihon/Aniyomi/Animetail restore; * gzipped `.tachibk`, or raw `.proto` (SyncYomi wire form) at compression level 0. * Novels and Yokan-only data are omitted. */ enum class BackupFormat { Yokan, Mangayomi, Tachiyomi } sealed interface BackupOpenResult { data class Ready(val plan: BackupPlan) : BackupOpenResult data class Failure(val error: BackupOpenError) : BackupOpenResult } enum class BackupOpenError { PasswordRequired, AuthenticationFailed, Invalid, Unsupported, } enum class RestoreMode { Replace, Merge, } @Serializable data class SafetySnapshot( val backupPath: String, val createdAt: Long, val description: String, ) class BackupPlan internal constructor( val preview: RestorePreview, internal val snapshot: BackupSnapshot, internal val includedSections: Set, internal val resolvedPassword: String? = null, val mode: RestoreMode = RestoreMode.Replace, ) { fun withMode(newMode: RestoreMode): BackupPlan = BackupPlan( preview = preview, snapshot = snapshot, includedSections = includedSections, resolvedPassword = resolvedPassword, mode = newMode, ) } data class BackupOptions( val includeManga: Boolean = true, val includeAnime: Boolean = true, val includeNovel: Boolean = true, val includeCategories: Boolean = true, val includeChapters: Boolean = true, val includeHistory: Boolean = true, val includeUpdates: Boolean = true, val includeSettings: Boolean = true, /** Tracker OAuth/credential rows; explicitly opt-in like Mangayomi's option 8. */ val includeSensitiveSettings: Boolean = false, val includeSources: Boolean = true, val includeTracks: Boolean = true, val includeCustomButtons: Boolean = true, val compressionLevel: Int? = null, val format: BackupFormat = BackupFormat.Yokan, val encrypt: Boolean = false, val password: String? = null, /** Cloud transports always disable this; native device backups include private text. */ val includePrivateNotes: Boolean = true, ) data class BackupResult( val fileName: String, val bytesWritten: Long, val itemCount: Int, val path: String? = null, ) data class BackupExport( val fileName: String, val bytes: ByteArray, val itemCount: Int, val format: BackupFormat, val compressed: Boolean, val contentType: String, ) { override fun equals(other: Any?): Boolean { if (this === other) return true if (other !is BackupExport) return false return fileName == other.fileName && bytes.contentEquals(other.bytes) && itemCount == other.itemCount && format == other.format && compressed == other.compressed && contentType == other.contentType } override fun hashCode(): Int { var result = fileName.hashCode() result = 31 * result + bytes.contentHashCode() result = 31 * result + itemCount result = 31 * result + format.hashCode() result = 31 * result + compressed.hashCode() result = 31 * result + contentType.hashCode() return result } } data class BackupFileEntry( val path: String, val fileName: String, val sizeBytes: Long, val lastModified: Long, val compressed: Boolean, ) data class RestorePreview( val itemCount: Int, val warnings: List, val mangaCount: Int = 0, val animeCount: Int = 0, val novelCount: Int = 0, val categoryCount: Int = 0, val chapterCount: Int = 0, val historyCount: Int = 0, val updateCount: Int = 0, val sourceCount: Int = 0, val trackCount: Int = 0, val newSeriesCount: Int = 0, val updatedSeriesCount: Int = 0, val newChapterCount: Int = 0, val conflictingCategories: List = emptyList(), val unmatchedSourceNames: List = emptyList(), ) const val BackupSchemaVersion: Int = 1 /** Native Yokan backup payload; also the decoded form of every external format. */ @Serializable data class BackupSnapshot( val schemaVersion: Int = BackupSchemaVersion, val createdAtMillis: Long, val sourceApp: String = "yokan-kmp", val includedSections: List? = null, val settings: DbSettings? = null, val mangas: List = emptyList(), val chapters: List = emptyList(), val categories: List = emptyList(), val sources: List = emptyList(), val histories: List = emptyList(), val updates: List = emptyList(), val tracks: List = emptyList(), val trackPreferences: List = emptyList(), val customButtons: List = emptyList(), val changedParts: List = emptyList(), /** False for cloud exports; replacement preserves matching local notes. Legacy backups replace normally. */ val privateNotesIncluded: Boolean = true, ) /** Section names of [BackupSnapshot.includedSections]; restore only touches included sections. */ internal object BackupSection { const val Settings = "settings" const val Manga = "manga" const val Anime = "anime" const val Novel = "novel" const val Chapters = "chapters" const val Categories = "categories" const val Sources = "sources" const val History = "history" const val Updates = "updates" const val Tracks = "tracks" const val TrackPreferences = "trackPreferences" const val CustomButtons = "customButtons" const val ChangedParts = "changedParts" val all = setOf( Settings, Manga, Anime, Novel, Chapters, Categories, Sources, History, Updates, Tracks, TrackPreferences, CustomButtons, ChangedParts, ) } /** Title types whose section is included. */ internal fun Set.titleTypes(): Set = buildSet { if (BackupSection.Manga in this@titleTypes) add(DbItemType.Manga) if (BackupSection.Anime in this@titleTypes) add(DbItemType.Anime) if (BackupSection.Novel in this@titleTypes) add(DbItemType.Novel) } /** JSON settings shared by every native backup reader and writer. */ internal val BackupJson = Json { ignoreUnknownKeys = true encodeDefaults = true prettyPrint = false }