import '../../models/document_model.dart'; enum DocumentSaveActionKind { putExisting, reuploadThenPut, blocked } /// Decides how an editor's save flow should reach the server, given whether /// a real server-assigned document is already resolved. Deliberately /// separated from the Dio call sites (`editor_screen.dart`, /// `product_editor_logic.dart`) so the guard against PUTting to a /// fabricated client-side ID - a millisecond timestamp that can never match /// the int4 `documents.id` (gap G5, `docs/api-contract-map.md`) - is /// unit-testable without mocking network I/O, mirroring the pure-decision /// pattern used by `poll_outcome.dart` (task 6.1). class DocumentSaveAction { final DocumentSaveActionKind kind; final String? serverId; final String? blockedMessage; const DocumentSaveAction._(this.kind, {this.serverId, this.blockedMessage}); factory DocumentSaveAction.putExisting(String serverId) => DocumentSaveAction._(DocumentSaveActionKind.putExisting, serverId: serverId); factory DocumentSaveAction.reuploadThenPut() => const DocumentSaveAction._(DocumentSaveActionKind.reuploadThenPut); factory DocumentSaveAction.blocked(String message) => DocumentSaveAction._(DocumentSaveActionKind.blocked, blockedMessage: message); } /// [existingDocument] is the server document already resolved from a /// completed upload+parse (e.g. `PendingDocument.document`) - when present, /// its real id is used for the PUT. When absent (e.g. the editor route lost /// its `pendingId` on process-death restoration), [localImagePath] - the /// pending item's originally-captured photo - lets the save flow recover a /// real server id by re-uploading it (safe: the backend dedups by /// `file_hash`) before PUTting the corrected fields. If neither is /// available there is nothing to save against, so saving is blocked with an /// explicit message instead of PUTting to a made-up ID that can never match /// a real document. DocumentSaveAction resolveDocumentSaveAction({ required DocumentModel? existingDocument, required String localImagePath, }) { if (existingDocument != null) { return DocumentSaveAction.putExisting(existingDocument.id); } if (localImagePath.isEmpty) { return DocumentSaveAction.blocked( 'Tidak dapat menyimpan: dokumen asli tidak ditemukan di perangkat. Silakan pindai ulang.', ); } return DocumentSaveAction.reuploadThenPut(); }