using System.Text.Json; using System.Text.Json.Nodes; using PlotLine.Models; namespace PlotLine.Services; internal static class ProjectBackupStoryMemory { internal static string? EntityTable(string? type) => type?.ToLowerInvariant() switch { "character" => "StoryMemoryCharacters", "asset" => "StoryMemoryAssets", "location" => "StoryMemoryLocations", _ => null }; private static string? Key(JsonObject node,string name) { var matches=node.Select(p=>p.Key).Where(key=>key.Equals(name,StringComparison.OrdinalIgnoreCase)).ToArray(); if(matches.Length>1)throw new JsonException("Profile evidence has ambiguous property names."); return matches.FirstOrDefault(); } private static IEnumerable EvidenceItems(JsonObject root) { foreach(var name in new[]{"EvidenceTimeline","Contradictions"}) { var key=Key(root,name); if(key is null || root[key] is null)continue; if(root[key] is not JsonArray items)throw new JsonException("Profile evidence must be an array."); foreach(var item in items) { if(item is not JsonObject evidence)throw new JsonException("Profile evidence records must be objects."); yield return evidence; } } } internal static string? Validate(ProjectBackupExport backup) { var memory=new Dictionary>(StringComparer.OrdinalIgnoreCase) { ["Character"]=backup.Data.StoryMemoryCharacters.Select(row=>row.StoryMemoryCharacterID).ToHashSet(), ["Asset"]=backup.Data.StoryMemoryAssets.Select(row=>row.StoryMemoryAssetID).ToHashSet(), ["Location"]=backup.Data.StoryMemoryLocations.Select(row=>row.StoryMemoryLocationID).ToHashSet() }; foreach(var row in backup.Data.StoryMemoryIllustrationAssignments) { if(string.IsNullOrEmpty(row.EntityType) || !memory.TryGetValue(row.EntityType,out var entities) || !entities.Contains(row.EntityID)) return "Project backup illustration assignment references a missing or unsupported memory entity."; var pipeline=row.EntityType.ToLowerInvariant() switch { "character" => backup.Data.StoryMemoryCharacters.Single(x=>x.StoryMemoryCharacterID==row.EntityID).ImportSessionID, "location" => backup.Data.StoryMemoryLocations.Single(x=>x.StoryMemoryLocationID==row.EntityID).ImportSessionID, _ => backup.Data.StoryMemoryAssets.Single(x=>x.StoryMemoryAssetID==row.EntityID).ImportSessionID }; if(pipeline!=row.ImportSessionID)return "Project backup illustration assignment belongs to a different memory pipeline."; } foreach(var row in backup.Data.StoryMemoryIllustrationDemands) if(EntityTable(row.EntityType) is null)return "Project backup illustration demand has an unsupported entity type."; foreach(var row in backup.Data.StoryMemoryRelationships) if(string.IsNullOrEmpty(row.SourceEntityType) || string.IsNullOrEmpty(row.TargetEntityType) || !memory.TryGetValue(row.SourceEntityType,out var source) || !source.Contains(row.SourceEntityID) || !memory.TryGetValue(row.TargetEntityType,out var target) || !target.Contains(row.TargetEntityID)) return "Project backup story-memory relationship references a missing or unsupported memory entity."; var results=backup.Data.StoryIntelligenceSceneResults.Select(row=>row.SceneResultID).ToHashSet(); try { foreach(var profile in backup.Data.CharacterIntelligenceProfiles) { if(string.IsNullOrWhiteSpace(profile.ProfileJson))return "Project backup character profile JSON is missing."; if(JsonNode.Parse(profile.ProfileJson) is not JsonObject root)return "Project backup character profile must contain a JSON object."; foreach(var item in EvidenceItems(root)) { var key=Key(item,"SceneResultID"); if(key is null || item[key] is null)continue; if(item[key] is not JsonValue value || !value.TryGetValue(out var id) || !results.Contains(id)) return "Project backup character profile evidence references a missing or foreign scene result."; } } } catch(JsonException) { return "Project backup character profile evidence JSON is invalid."; } return null; } internal static string RemapProfile(string json,Func remap) { var root=JsonNode.Parse(json)!.AsObject(); foreach(var item in EvidenceItems(root)) { var key=Key(item,"SceneResultID"); if(key is not null && item[key] is JsonValue value && value.TryGetValue(out var id))item[key]=remap(id); } return root.ToJsonString(); } }