namespace PlotLine.Services.Writer; // A projection of already-authorised selections, never an authorisation or Word navigation command. // Each surface owns its slot; selecting a reference must not replace the author's working focus. public sealed record WriterSelection { public int ProjectID { get; } public int BookID { get; } public int? ChapterID { get; } public int? SceneID { get; } public WriterSelection(int projectID, int bookID, int? chapterID = null, int? sceneID = null) { if (projectID <= 0 || bookID <= 0 || chapterID is <= 0 || sceneID is <= 0 || (sceneID.HasValue && !chapterID.HasValue)) throw new ArgumentException("A scene selection requires a chapter in a positive project/book scope."); ProjectID = projectID; BookID = bookID; ChapterID = chapterID; SceneID = sceneID; } } public enum WriterFocusRole { Working, SavedObjective, Exploration, PinnedReference, Reader, Session } public sealed record WriterNativePosition(WriterSelection Selection, Guid Instance, Guid DocumentSession, Guid Manuscript, long ObservedRevision, long AcceptedRevision); public sealed record WriterContext { public WriterSelection Scope { get; } public WriterSelection? Working { get; private init; } public WriterSelection? SavedObjective { get; private init; } public WriterSelection? Exploration { get; private init; } public WriterSelection? PinnedReference { get; private init; } public WriterSelection? Reader { get; private init; } public WriterSelection? Session { get; private init; } public WriterNativePosition? VerifiedWord { get; private init; } public WriterContext(int projectID, int bookID) => Scope = new(projectID, bookID); public WriterContext Select(WriterFocusRole role, WriterSelection selection) { CheckScope(selection); return role switch { WriterFocusRole.Working => this with { Working = selection }, WriterFocusRole.SavedObjective => this with { SavedObjective = selection }, WriterFocusRole.Exploration => this with { Exploration = selection }, WriterFocusRole.PinnedReference => this with { PinnedReference = selection }, WriterFocusRole.Reader => this with { Reader = selection }, WriterFocusRole.Session => this with { Session = selection }, _ => throw new ArgumentOutOfRangeException(nameof(role)) }; } // Called only after the existing native lease, saved-document, accepted-position and race checks. public WriterContext WithVerifiedWord(WriterNativePosition position) { CheckScope(position.Selection); if (position.Instance == Guid.Empty || position.DocumentSession == Guid.Empty || position.Manuscript == Guid.Empty || position.ObservedRevision < 0 || position.AcceptedRevision <= 0) throw new ArgumentException("Verified native position requires complete document and accepted revision identity."); return this with { VerifiedWord = position }; } private void CheckScope(WriterSelection selection) { if (selection.ProjectID != Scope.ProjectID || selection.BookID != Scope.BookID) throw new ArgumentException("Selections from different books require a new context."); } }