using System.Text.Json; using PlotLine.Data; using PlotLine.Models; using PlotLine.ViewModels; namespace PlotLine.Services; public interface IStoryIntelligenceExceptionReviewService { Task AssessPreparedAsync(int bookId, string module); Task ReassessAsync(OnboardingStoryIntelligenceBatch batch, string module); Task> SummaryAsync(int bookId); Task> ListAsync(int bookId, string module); } public sealed class StoryIntelligenceExceptionReviewService( IStoryIntelligenceReviewCandidateRepository repository, IStoryIntelligenceKnowledgeImportService knowledge, IStoryIntelligenceRelationshipImportService relationships, IStoryIntelligenceAssetImportService assets) : IStoryIntelligenceExceptionReviewService { public static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web); public Task> SummaryAsync(int bookId) => repository.GetReviewSummaryAsync(bookId); public Task> ListAsync(int bookId, string module) => repository.ListAllAsync(bookId, module); public Task AssessPreparedAsync(int bookId, string module) => AssessAsync(bookId, module, new Dictionary()); // Explicit POST only: recover richer evidence from stored scene JSON, never call an AI client. // Match stable keys in place. Unmatched older proposals stay visible and optional, never guessed or deleted. public async Task ReassessAsync(OnboardingStoryIntelligenceBatch batch, string module) { Dictionary payloads = module switch { StoryIntelligenceReviewModules.Knowledge => (await knowledge.BuildReviewAsync(batch)).Candidates.ToDictionary(x => x.Key, x => JsonSerializer.Serialize(x, JsonOptions)), StoryIntelligenceReviewModules.Relationships => (await relationships.BuildReviewAsync(batch)).Candidates.ToDictionary(x => x.Key, x => JsonSerializer.Serialize(x, JsonOptions)), StoryIntelligenceReviewModules.Assets => (await assets.BuildReviewAsync(batch)).Candidates.ToDictionary(x => x.Key, x => JsonSerializer.Serialize(x, JsonOptions)), _ => throw new ArgumentException("Unsupported review category.", nameof(module)) }; await AssessAsync(batch.BookID, module, payloads); } private async Task AssessAsync(int bookId, string module, IReadOnlyDictionary refreshed) { if (module is not (StoryIntelligenceReviewModules.Knowledge or StoryIntelligenceReviewModules.Relationships or StoryIntelligenceReviewModules.Assets)) return; var rows = await repository.ListAllAsync(bookId, module); string Payload(StoryIntelligenceReviewCandidateRow row) => row.DecisionStatus == "Pending" && refreshed.TryGetValue(row.StableKey, out var json) ? json : row.CandidatePayloadJson; // Ignored proposals are not evidence against a later observation. Reviewed proposals may // have been corrected in canonical editors; do not reinterpret their original AI state. var contextRows = rows.Where(x => x.DecisionStatus is "Pending" or "AutoAccepted").ToList(); var knowledgeContext = module == StoryIntelligenceReviewModules.Knowledge ? contextRows.Select(x => JsonSerializer.Deserialize(Payload(x), JsonOptions)!).ToList() : []; var relationshipContext = module == StoryIntelligenceReviewModules.Relationships ? contextRows.Select(x => JsonSerializer.Deserialize(Payload(x), JsonOptions)!).ToList() : []; // Classify the whole snapshot before accepting any item; conflicts cannot disappear as rows are accepted. var assessments = rows.Where(x => x.DecisionStatus == "Pending").Select(row => { var payload = Payload(row); var assessment = module switch { StoryIntelligenceReviewModules.Knowledge => StoryIntelligenceReviewPolicy.Knowledge(JsonSerializer.Deserialize(payload, JsonOptions)!, knowledgeContext), StoryIntelligenceReviewModules.Relationships => StoryIntelligenceReviewPolicy.Relationship(JsonSerializer.Deserialize(payload, JsonOptions)!, relationshipContext), _ => StoryIntelligenceReviewPolicy.Asset(JsonSerializer.Deserialize(payload, JsonOptions)!) }; return (Row: row, Payload: payload, Assessment: assessment); }).ToList(); foreach (var item in assessments) await repository.AssessAsync(item.Row.ReviewCandidateID, item.Row.ReviewCandidateGenerationID, item.Payload, JsonSerializer.Serialize(item.Assessment, JsonOptions)); } }