using System.Text.RegularExpressions; using PlotLine.Models; using PlotLine.ViewModels; namespace PlotLine.Services; /// Deterministic assessment of saved evidence, never an AI request or a confidence-only filter. public static class StoryIntelligenceReviewPolicy { // Matches the existing extraction UI's High band. Evidence and identity gates are also mandatory. public const decimal HighConfidence = .75m; private static string Normalise(string? value) => Regex.Replace(value?.ToLowerInvariant() ?? "", @"[^\p{L}\p{N}]+", " ").Trim(); private static bool Consequential(string? text) => Regex.IsMatch(text ?? "", @"\b(betray\w*|murder\w*|kill\w*|confess\w*|blackmail\w*|expos\w*|revelation|secret identity|divorc\w*|break.?up|rescu\w*|threat\w*)\b", RegexOptions.IgnoreCase); private static bool Supported(IReadOnlyList observations) => observations.Count > 0 && observations.All(x => x.SceneID > 0 && x.Confidence >= HighConfidence && x.Confidence <= 1m && !string.IsNullOrWhiteSpace(x.Evidence)); private static StoryIntelligenceReviewAssessment Result(string priority, params string[] reasons) => new(priority, reasons); public static StoryIntelligenceReviewAssessment Knowledge(StoryIntelligenceKnowledgeReviewCandidateViewModel item, IReadOnlyList context) { var significant = Consequential(item.KnowledgeStatement + " " + item.Evidence); if (item.AmbiguousIdentity) return Result(StoryIntelligenceReviewAssessment.Attention, "More than one character shares this name or alias. Confirm the identity before using this knowledge."); var conflicting = context.Any(other => other.Key != item.Key && other.CharacterID == item.CharacterID && Normalise(other.KnowledgeStatement) == Normalise(item.KnowledgeStatement) && other.KnowledgeStateName != item.KnowledgeStateName && other.Observations.Any(a => item.Observations.Any(b => a.SceneID == b.SceneID)) && (other.KnowledgeStateName == "Unaware" || item.KnowledgeStateName == "Unaware")); if (conflicting) return Result(StoryIntelligenceReviewAssessment.Attention, "The same scene suggests both awareness and lack of awareness of this fact. Check the order of events."); // Beliefs, hearsay and inference stay explicitly different from established knowledge. var subjective = item.KnowledgeStateName is "Suspects" or "Misunderstands" or "Partially Knows" || item.Observations.Any(x => Regex.IsMatch(x.Signal ?? "", "told|infer|believ|suspect|misunderstand", RegexOptions.IgnoreCase)); if (significant && (subjective || !Supported(item.Observations))) return Result(StoryIntelligenceReviewAssessment.Attention, "This consequential information depends on an uncertain knowledge state or incomplete evidence."); if (subjective) return Result(StoryIntelligenceReviewAssessment.Optional, "A belief, suspicion or reported fact may benefit from your interpretation; it is not treated as established knowledge."); if (item.CharacterID > 0 && item.KnowledgeStateID > 0 && Supported(item.Observations) && item.Observations.All(x => Regex.IsMatch(x.Signal ?? "", "^(Learns|Realises|Realizes|Discovers|Confirms|Knows)$", RegexOptions.IgnoreCase))) return Result(StoryIntelligenceReviewAssessment.Tracked, "A resolved character and explicit scene evidence support this knowledge state."); return Result(StoryIntelligenceReviewAssessment.Optional, "The saved evidence does not establish enough detail for automatic acceptance. Review when useful."); } private static string Track(StoryIntelligenceRelationshipReviewCandidateViewModel x) => x.IsReciprocal ? $"{Math.Min(x.CharacterAID, x.CharacterBID)}:{Math.Max(x.CharacterAID, x.CharacterBID)}:mutual:{x.RelationshipType}" : $"{x.CharacterAID}>{x.CharacterBID}:{x.RelationshipType}"; private static int Polarity(string state) => Normalise(state) switch { "friendly" or "trusting" or "close" or "romantic" or "deep attachment" => 1, "hostile" or "enemy" or "enemies" or "distrustful" => -1, _ => 0 }; public static StoryIntelligenceReviewAssessment Relationship(StoryIntelligenceRelationshipReviewCandidateViewModel item, IReadOnlyList context) { if (item.AmbiguousIdentity) return Result(StoryIntelligenceReviewAssessment.Attention, "More than one character shares a participant's name or alias. Confirm who is involved."); var significant = Consequential(item.Development + " " + item.ExampleContext) || item.ProposalKind == "Factual"; if (item.NeedsDirectionReview) return Result(significant ? StoryIntelligenceReviewAssessment.Attention : StoryIntelligenceReviewAssessment.Optional, "The saved analysis does not establish direction or whether this feeling is mutual."); var track = context.Where(x => Track(x) == Track(item)).OrderBy(x => x.FirstChapterNumber) .ThenBy(x => x.FirstSceneNumber).ThenBy(x => x.FirstSceneID).ToList(); if (track.Any(x => x.Key != item.Key && x.FirstSceneID == item.FirstSceneID && Polarity(x.RelationshipState) * Polarity(item.RelationshipState) == -1)) return Result(StoryIntelligenceReviewAssessment.Attention, "This scene contains opposing interpretations of the same directional relationship."); var index = track.FindIndex(x => x.Key == item.Key); if (index > 0 && index < track.Count - 1 && Polarity(track[index - 1].RelationshipState) == Polarity(track[index + 1].RelationshipState) && Polarity(item.RelationshipState) * Polarity(track[index - 1].RelationshipState) == -1 && !Consequential(item.Development + " " + item.ExampleContext) && !Supported(item.Observations)) return Result(StoryIntelligenceReviewAssessment.Attention, "An opposing state between surrounding observations has weak support. Check the transition in context."); if (significant && !Supported(item.Observations)) return Result(StoryIntelligenceReviewAssessment.Attention, "A major development or factual tie has incomplete supporting evidence."); if (item.CharacterAID > 0 && item.CharacterBID > 0 && item.CharacterAID != item.CharacterBID && item.RelationshipStateID > 0 && item.RelationshipTypeID > 0 && item.FirstSceneID > 0 && !string.IsNullOrWhiteSpace(item.Development) && Supported(item.Observations) && item.HasExplicitType && (item.ProposalKind == "Factual" || (item.HasExplicitState && !string.Equals(item.RelationshipState, "Unknown", StringComparison.OrdinalIgnoreCase)))) return Result(StoryIntelligenceReviewAssessment.Tracked, "Scene evidence supports the identified participants, direction and relationship state."); return Result(StoryIntelligenceReviewAssessment.Optional, "The interpretation or supporting detail is uncertain. Review if useful to your story."); } public static StoryIntelligenceReviewAssessment Asset(StoryIntelligenceAssetReviewCandidateViewModel item) => Result(Consequential(item.ExampleContext) && item.Confidence != "High" ? StoryIntelligenceReviewAssessment.Attention : StoryIntelligenceReviewAssessment.Optional, "Existing exact matches are linked automatically. Confirm a new asset's identity or ownership when it matters to the story."); }