package eu.friedcutlet.yokan.ui.screens.local import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.padding import androidx.compose.material3.AlertDialog import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import eu.friedcutlet.yokan.domain.Chapter import eu.friedcutlet.yokan.domain.Manga import eu.friedcutlet.yokan.services.library.LocalEntryManagementOperation import eu.friedcutlet.yokan.services.library.LocalEntryManagementPreview import eu.friedcutlet.yokan.services.library.LocalLibraryDuplicateGroup import org.jetbrains.compose.resources.stringResource import yokan.composeapp.generated.resources.Res import yokan.composeapp.generated.resources.action_cancel /** Read-only detail of one indexed local entry: source path, metadata link, reconciliation state and chapters. */ @Composable internal fun LocalEntryDetailContent( manga: Manga, stale: Boolean, chapters: List, modifier: Modifier, onReviewMetadata: () -> Unit, canColorize: Boolean = false, onColorize: () -> Unit = {}, ) { Column(modifier) { Text(manga.name, style = MaterialTheme.typography.titleLarge) Text("Source path", style = MaterialTheme.typography.titleSmall) Text(manga.link.ifBlank { "Unavailable" }) Text("Media type: ${manga.itemType}") Text("Metadata: ${if (manga.metadataServerId == null) "unmatched" else "linked (${manga.metadataSource})"}") TextButton(onClick = onReviewMetadata) { Text("Review metadata") } if (canColorize) TextButton(onClick = onColorize) { Text("Create colorized copy") } Text("Reconciliation: ${if (stale) "stale" else "no title issue reported"}") Text("Chapters", style = MaterialTheme.typography.titleSmall, modifier = Modifier.padding(top = 12.dp)) chapters.forEach { chapter -> Text("${chapter.name} ยท ${chapter.url}", modifier = Modifier.padding(vertical = 4.dp)) } } } /** Picks the canonical database entry of an exact-identity duplicate group; [onChoose] receives its id. */ @Composable internal fun LocalDuplicateGroupDialog( group: LocalLibraryDuplicateGroup, titleName: (Long) -> String?, onChoose: (Long) -> Unit, onDismiss: () -> Unit, ) { AlertDialog( onDismissRequest = onDismiss, title = { Text("Choose canonical entry") }, text = { Column { Text("Only exact local identities can be merged. Source files affected: 0.") group.ids.forEach { id -> TextButton(onClick = { onChoose(id) }) { Text(titleName(id) ?: "Entry $id") } } } }, confirmButton = {}, ) } /** Confirms a previewed forget/merge operation, showing its database impact and any blockers. */ @Composable internal fun LocalEntryOperationDialog( preview: LocalEntryManagementPreview, onConfirm: () -> Unit, onDismiss: () -> Unit, ) { val isMerge = preview.command.operation is LocalEntryManagementOperation.MergeDuplicateTitles AlertDialog( onDismissRequest = onDismiss, title = { Text(if (isMerge) "Review merge" else "Review forgetting entry") }, text = { Column { Text("Database rows removed: ${preview.databaseRowsRemoved}") Text("Database rows updated: ${preview.databaseRowsUpdated}") Text("Source files affected: ${preview.sourceFilesAffected}") if (preview.blockers.isNotEmpty()) { Text("Blocked: ${preview.blockers.joinToString { localBlockerLabel(it) }}", color = MaterialTheme.colorScheme.error) } else if (!isMerge) { Text("Forget from database only. Source files remain unchanged.") } } }, confirmButton = { TextButton( enabled = preview.blockers.isEmpty(), onClick = onConfirm, ) { Text(if (isMerge) "Merge entries" else "Forget from database") } }, dismissButton = { TextButton(onClick = onDismiss) { Text(stringResource(Res.string.action_cancel)) } }, ) } private fun localBlockerLabel(blocker: String): String = when (blocker) { "STATE_CHANGED" -> "the entry changed; review again" "FRESH_RECONCILIATION_CHANGED" -> "fresh reconciliation is required" "STALE_EVIDENCE_REQUIRED" -> "stale evidence is required" "CANDIDATE_CHANGED" -> "the stale candidate changed" "ACTIVE_DOWNLOAD" -> "an active download is using this entry" "INVALID_DUPLICATE_GROUP" -> "this is not an exact duplicate group" "TORRENT_ENTRY" -> "torrent entries cannot be merged" "ITEM_TYPE_MISMATCH" -> "media types do not match" "REMOTE_METADATA_CONFLICT" -> "metadata sources conflict" "TRACKER_BINDING_CONFLICT" -> "tracker links conflict" "ATOMIC_SNAPSHOT_PERSISTENCE_UNAVAILABLE" -> "atomic database updates are unavailable" "ATOMIC_SNAPSHOT_PERSISTENCE_FAILED" -> "the database update failed safely" else -> blocker }