using System.Security.Cryptography; using System.Text.Json; using Microsoft.AspNetCore.DataProtection; using PlotDirector.Companion.Protocol; using PlotLine.Data; namespace PlotLine.Services.NativeCompanion; // Historical offsets require authenticated captured content and its original live session. // SourceSession/SourceRevision are optional model fields, not columns returned by the review procedure. public sealed class NativeReviewNavigation(IDataProtectionProvider protection) { public const string Unavailable = "The original Word position can no longer be verified. Review decisions remain available. Locate this passage directly in Word."; private readonly IDataProtector source = protection.CreateProtector("NativeImport.Source.v1"); public NativeLease[] Candidates(int author, NativeReview row, AiChapterResult result, IEnumerable leases) { var capture = Source(author, row, result); if (capture == null) return []; var matching = leases.Where(l => l.Session == capture.SessionId).Take(2).ToArray(); return matching.Length == 1 && Live(author, row, capture, matching[0]) ? matching : []; } public bool Current(int author, NativeReview row, AiChapterResult result, NativeLease lease) => Source(author, row, result) is {} capture && Live(author, row, capture, lease); private static bool Live(int author, NativeReview row, AiChapterRequest capture, NativeLease lease) => lease.User == author && lease.Instance != Guid.Empty && lease.Session == capture.SessionId && lease.Position.Revision == capture.Revision && lease.Position.BookId == row.BookID && lease.Position.ManuscriptId == row.ManuscriptID && lease.Position.WordAvailable && lease.Position.WordSaved == true && lease.Position.CanAcknowledgeNavigation; private AiChapterRequest? Source(int author, NativeReview row, AiChapterResult result) { if (row.UserID != author || row.BookID <= 0 || row.ManuscriptID == Guid.Empty || result.Scenes == null || result.Scenes.Length == 0 || string.IsNullOrEmpty(row.EncryptedSource)) return null; try { var capture = JsonSerializer.Deserialize(source.Unprotect(row.EncryptedSource)); if (capture == null || capture.SessionId == Guid.Empty || capture.Revision < 0 || capture.Revision != result.DocumentRevision || row.SourceSession.HasValue && row.SourceSession != capture.SessionId || row.SourceRevision.HasValue && row.SourceRevision != capture.Revision || capture.BookId != row.BookID || capture.ManuscriptId != row.ManuscriptID || capture.ChapterStart != row.ChapterStart || capture.ChapterEnd != row.ChapterEnd) return null; // Use the existing native API upper bound; a retained valid capture may exceed its default analysis size. NativeChapterMapping.Build(capture,500000); var hash = Convert.ToHexString(SHA256.HashData(JsonSerializer.SerializeToUtf8Bytes(new { capture.ChapterStart, capture.ChapterEnd, capture.Paragraphs }))); return hash == row.ContentHash && result.Scenes.All(scene => scene != null && scene.Start >= capture.ChapterStart && scene.End <= capture.ChapterEnd && scene.End > scene.Start && capture.Paragraphs.Any(p => p.Start == scene.Start) && capture.Paragraphs.Any(p => p.End == scene.End)) ? capture : null; } catch (Exception e) when (e is CryptographicException or JsonException or ArgumentException) { return null; } } }