PlotDirector/PlotLine/Services/ProjectBackupGraphValidation.cs

148 lines
25 KiB
C#

using System.Collections;
using PlotLine.Models;
namespace PlotLine.Services;
// Explicit persisted relationships for the current serialized entity sets, not inferred author names.
internal static class ProjectBackupGraphValidation
{
internal sealed record Link(string Column, string TargetCollection, string TargetColumn);
internal sealed record Entity(string Collection, string? Identity, params Link[] Links);
internal static readonly Entity[] Entities = [
new("StoryMemoryIllustrationAssignments","StoryMemoryIllustrationAssignmentID",new Link("ImportSessionID","StoryIntelligenceBookPipelines","StoryIntelligenceBookPipelineID")),
new("StoryMemoryIllustrationDemands","StoryMemoryIllustrationDemandID",new Link("ImportSessionID","StoryIntelligenceBookPipelines","StoryIntelligenceBookPipelineID"),new Link("ProjectID","Project","ProjectID")),
new("StoryIntelligenceIllustrationDemands","StoryIntelligenceIllustrationDemandID",new Link("ProjectID","Project","ProjectID")),
new("WholeBookPlotSynthesisRuns","WholeBookPlotSynthesisRunID",new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("SourceStoryIntelligenceRunID","StoryIntelligenceRuns","StoryIntelligenceRunID")),
new("StoryIntelligenceNarrativeCandidates","NarrativeCandidateID",new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("ExistingPlotLineID","PlotLines","PlotLineID"),new Link("ExistingPlotThreadID","PlotThreads","PlotThreadID"),new Link("RelatedPlotLineID","PlotLines","PlotLineID"),new Link("AcceptedPlotLineID","PlotLines","PlotLineID"),new Link("AcceptedPlotThreadID","PlotThreads","PlotThreadID"),new Link("ParentNarrativeCandidateID","StoryIntelligenceNarrativeCandidates","NarrativeCandidateID"),new Link("MergeTargetNarrativeCandidateID","StoryIntelligenceNarrativeCandidates","NarrativeCandidateID"),new Link("WholeBookPlotSynthesisRunID","WholeBookPlotSynthesisRuns","WholeBookPlotSynthesisRunID"),new Link("AuthorPlotSeedID","AuthorPlotSeeds","AuthorPlotSeedID")),
new("StoryIntelligenceNarrativeCandidateEvidence","NarrativeCandidateEvidenceID",new Link("NarrativeCandidateID","StoryIntelligenceNarrativeCandidates","NarrativeCandidateID"),new Link("SceneID","Scenes","SceneID")),
new("StoryIntelligenceNarrativeCandidateEvents","NarrativeCandidateEventID",new Link("NarrativeCandidateID","StoryIntelligenceNarrativeCandidates","NarrativeCandidateID"),new Link("WholeBookPlotSynthesisRunID","WholeBookPlotSynthesisRuns","WholeBookPlotSynthesisRunID"),new Link("SceneID","Scenes","SceneID"),new Link("AcceptedThreadEventID","ThreadEvents","ThreadEventID")),
new("CharacterEnrichmentRuns","CharacterEnrichmentRunID",new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("StoryIntelligenceBookPipelineID","StoryIntelligenceBookPipelines","StoryIntelligenceBookPipelineID"),new Link("CharacterID","Characters","CharacterID")),
new("CharacterEnrichmentEvidence","CharacterEnrichmentEvidenceID",new Link("CharacterEnrichmentRunID","CharacterEnrichmentRuns","CharacterEnrichmentRunID"),new Link("CharacterID","Characters","CharacterID"),new Link("SceneID","Scenes","SceneID")),
new("StoryIntelligenceReviewCandidateGenerations","ReviewCandidateGenerationID",new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID")),
new("StoryIntelligenceReviewCandidates","ReviewCandidateID",new Link("ReviewCandidateGenerationID","StoryIntelligenceReviewCandidateGenerations","ReviewCandidateGenerationID"),new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID")),
new("StoryIntelligenceReviewCandidateEvidence","ReviewCandidateEvidenceID",new Link("ReviewCandidateID","StoryIntelligenceReviewCandidates","ReviewCandidateID"),new Link("SceneID","Scenes","SceneID")),
new("StoryIntelligenceBookPipelines","StoryIntelligenceBookPipelineID",new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("LastRunID","StoryIntelligenceRuns","StoryIntelligenceRunID")),
new("StoryMemoryCharacters","StoryMemoryCharacterID",new Link("ImportSessionID","StoryIntelligenceBookPipelines","StoryIntelligenceBookPipelineID"),new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("CreatedSceneResultID","StoryIntelligenceSceneResults","SceneResultID"),new Link("LastUpdatedSceneResultID","StoryIntelligenceSceneResults","SceneResultID")),
new("StoryMemoryCharacterAliases","StoryMemoryCharacterAliasID",new Link("StoryMemoryCharacterID","StoryMemoryCharacters","StoryMemoryCharacterID"),new Link("SourceSceneResultID","StoryIntelligenceSceneResults","SceneResultID")),
new("StoryMemoryCharacterAttributes","StoryMemoryCharacterAttributeID",new Link("StoryMemoryCharacterID","StoryMemoryCharacters","StoryMemoryCharacterID"),new Link("SourceSceneResultID","StoryIntelligenceSceneResults","SceneResultID")),
new("StoryMemoryLocations","StoryMemoryLocationID",new Link("ImportSessionID","StoryIntelligenceBookPipelines","StoryIntelligenceBookPipelineID"),new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("FirstSeenSceneResultID","StoryIntelligenceSceneResults","SceneResultID"),new Link("LastSeenSceneResultID","StoryIntelligenceSceneResults","SceneResultID")),
new("StoryMemoryAssets","StoryMemoryAssetID",new Link("ImportSessionID","StoryIntelligenceBookPipelines","StoryIntelligenceBookPipelineID"),new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("FirstSeenSceneResultID","StoryIntelligenceSceneResults","SceneResultID"),new Link("LastSeenSceneResultID","StoryIntelligenceSceneResults","SceneResultID")),
new("StoryMemoryRelationships","StoryMemoryRelationshipID",new Link("ImportSessionID","StoryIntelligenceBookPipelines","StoryIntelligenceBookPipelineID"),new Link("FirstSeenSceneResultID","StoryIntelligenceSceneResults","SceneResultID"),new Link("LastUpdatedSceneResultID","StoryIntelligenceSceneResults","SceneResultID")),
new("StoryMemoryProcessedSceneResults","StoryMemoryProcessedSceneResultID",new Link("ImportSessionID","StoryIntelligenceBookPipelines","StoryIntelligenceBookPipelineID"),new Link("SceneResultID","StoryIntelligenceSceneResults","SceneResultID")),
new("StoryMemoryImportPreferences","StoryMemoryImportPreferenceID",new Link("ImportSessionID","StoryIntelligenceBookPipelines","StoryIntelligenceBookPipelineID")),
new("CharacterIntelligenceProfiles","CharacterIntelligenceProfileID",new Link("ImportSessionID","StoryIntelligenceBookPipelines","StoryIntelligenceBookPipelineID"),new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("LastUpdatedSceneResultID","StoryIntelligenceSceneResults","SceneResultID")),
new("CharacterIntelligenceEvidenceEvents","CharacterIntelligenceEvidenceEventID",new Link("CharacterIntelligenceProfileID","CharacterIntelligenceProfiles","CharacterIntelligenceProfileID"),new Link("SceneResultID","StoryIntelligenceSceneResults","SceneResultID")),
new("StoryIntelligenceRuns","StoryIntelligenceRunID",new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("ChapterID","Chapters","ChapterID")),
new("StoryIntelligenceChapterResults","ChapterResultID",new Link("StoryIntelligenceRunID","StoryIntelligenceRuns","StoryIntelligenceRunID"),new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("ChapterID","Chapters","ChapterID")),
new("StoryIntelligenceSceneResults","SceneResultID",new Link("StoryIntelligenceRunID","StoryIntelligenceRuns","StoryIntelligenceRunID"),new Link("ChapterResultID","StoryIntelligenceChapterResults","ChapterResultID"),new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("ChapterID","Chapters","ChapterID"),new Link("SceneID","Scenes","SceneID")),
new("StoryIntelligenceImportCommits","StoryIntelligenceImportCommitID",new Link("StoryIntelligenceRunID","StoryIntelligenceRuns","StoryIntelligenceRunID"),new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("ChapterID","Chapters","ChapterID")),
new("CoreImportBasicLocationReviews","CoreImportBasicLocationReviewID",new Link("BookID","Books","BookID"),new Link("ProjectID","Project","ProjectID"),new Link("BuildID","OnboardingManuscriptBuilds","OnboardingManuscriptBuildID")),
new("CoreImportBasicLocationCandidates","CoreImportBasicLocationCandidateID",new Link("ExistingLocationID","Locations","LocationID"),new Link("ParentLocationID","Locations","LocationID"),new Link("ResultLocationID","Locations","LocationID"),new Link("CoreImportBasicLocationReviewID","CoreImportBasicLocationReviews","CoreImportBasicLocationReviewID"),new Link("SameAsLocationID","Locations","LocationID")),
new("CoreImportBasicLocationEvidence","CoreImportBasicLocationEvidenceID",new Link("CoreImportBasicLocationCandidateID","CoreImportBasicLocationCandidates","CoreImportBasicLocationCandidateID"),new Link("SceneID","Scenes","SceneID"),new Link("ChapterID","Chapters","ChapterID")),
new("SceneAssetSuggestions","SceneAssetSuggestionID",new Link("SceneID","Scenes","SceneID"),new Link("AssetID","StoryAssets","StoryAssetID")),
new("SceneCharacterSuggestions","SceneCharacterSuggestionID",new Link("CharacterID","Characters","CharacterID"),new Link("SceneID","Scenes","SceneID")),
new("SceneLocationSuggestions","SceneLocationSuggestionID",new Link("LocationID","Locations","LocationID"),new Link("SceneID","Scenes","SceneID")),
new("SceneTextStatistics",null,new Link("SceneID","Scenes","SceneID")),
new("OnboardingManuscriptBuilds","OnboardingManuscriptBuildID",new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("ManuscriptDocumentID","ManuscriptDocuments","ManuscriptDocumentID")),
new("OnboardingManuscriptBuildChapterMap",null,new Link("OnboardingManuscriptBuildID","OnboardingManuscriptBuilds","OnboardingManuscriptBuildID"),new Link("ChapterID","Chapters","ChapterID")),
new("OnboardingManuscriptBuildSceneMap",null,new Link("OnboardingManuscriptBuildID","OnboardingManuscriptBuilds","OnboardingManuscriptBuildID"),new Link("SceneID","Scenes","SceneID")),
new("OnboardingManuscriptBuildCharacterMap",null,new Link("OnboardingManuscriptBuildID","OnboardingManuscriptBuilds","OnboardingManuscriptBuildID"),new Link("CharacterID","Characters","CharacterID")),
new("CharacterImages","CharacterImageID",new Link("CharacterID","Characters","CharacterID"),new Link("ProjectID","Project","ProjectID")),
new("WordCompanionSceneAssets","WordCompanionSceneAssetID",new Link("SceneID","Scenes","SceneID"),new Link("StoryAssetID","StoryAssets","StoryAssetID")),
new("WordCompanionSceneLocations","WordCompanionSceneLocationID",new Link("SceneID","Scenes","SceneID"),new Link("LocationID","Locations","LocationID")),
new("AssetAliases", "AssetAliasID", new Link("StoryAssetID", "StoryAssets", "StoryAssetID")),
new("AssetCustodyCharacters", "AssetCustodyEventCharacterID", new Link("AssetCustodyEventID", "AssetCustodyEvents", "AssetCustodyEventID"), new Link("CharacterID", "Characters", "CharacterID")),
new("AssetCustodyEvents", "AssetCustodyEventID", new Link("SceneID", "Scenes", "SceneID"), new Link("StoryAssetID", "StoryAssets", "StoryAssetID")),
new("AssetDependencies", "AssetDependencyID", new Link("SourceAssetID", "StoryAssets", "StoryAssetID"), new Link("SourceRequiredStateID", "AssetStates", "AssetStateID"), new Link("TargetAssetID", "StoryAssets", "StoryAssetID"), new Link("TargetBlockedStateID", "AssetStates", "AssetStateID")),
new("AssetEvents", "AssetEventID", new Link("FromStateID", "AssetStates", "AssetStateID"), new Link("SceneID", "Scenes", "SceneID"), new Link("StoryAssetID", "StoryAssets", "StoryAssetID"), new Link("ToStateID", "AssetStates", "AssetStateID")),
new("AssetStates", "AssetStateID", new Link("ProjectID", "Project", "ProjectID")),
new("AuthorPlotSeeds", "AuthorPlotSeedID", new Link("BookID", "Books", "BookID"), new Link("MatchedPlotLineID", "PlotLines", "PlotLineID"), new Link("ProjectID", "Project", "ProjectID")),
new("Books", "BookID", new Link("ProjectID", "Project", "ProjectID")),
new("ChapterGoals", "ChapterGoalID", new Link("ChapterID", "Chapters", "ChapterID")),
new("Chapters", "ChapterID", new Link("BookID", "Books", "BookID"), new Link("ManuscriptDocumentID", "ManuscriptDocuments", "ManuscriptDocumentID"), new Link("POVCharacterID", "Characters", "CharacterID")),
new("CharacterAliases", "CharacterAliasID", new Link("CharacterID", "Characters", "CharacterID")),
new("CharacterAttributeEvents", "CharacterAttributeEventID", new Link("CharacterAttributeTypeID", "CharacterAttributeTypes", "CharacterAttributeTypeID"), new Link("CharacterID", "Characters", "CharacterID"), new Link("SceneID", "Scenes", "SceneID")),
new("CharacterAttributeTypes", "CharacterAttributeTypeID", new Link("ProjectID", "Project", "ProjectID")),
new("CharacterKnowledge", "CharacterKnowledgeID", new Link("CharacterID", "Characters", "CharacterID"), new Link("PlotThreadID", "PlotThreads", "PlotThreadID"), new Link("SceneID", "Scenes", "SceneID"), new Link("SourceEventID", "ThreadEvents", "ThreadEventID"), new Link("StoryAssetID", "StoryAssets", "StoryAssetID")),
new("CharacterRelationships", "CharacterRelationshipID", new Link("CharacterAID", "Characters", "CharacterID"), new Link("CharacterBID", "Characters", "CharacterID"), new Link("EndSceneID", "Scenes", "SceneID"), new Link("InitialBookID", "Books", "BookID"), new Link("ProjectID", "Project", "ProjectID"), new Link("StartSceneID", "Scenes", "SceneID")),
new("Characters", "CharacterID", new Link("AvatarSourceCharacterImageID","CharacterImages","CharacterImageID"), new Link("AgeReferenceSceneID", "Scenes", "SceneID"), new Link("ProjectID", "Project", "ProjectID")),
new("ContinuityWarningAcknowledgements", "ContinuityWarningAcknowledgementID", new Link("AssetID", "StoryAssets", "StoryAssetID"), new Link("CharacterID", "Characters", "CharacterID"), new Link("ProjectID", "Project", "ProjectID"), new Link("SceneID", "Scenes", "SceneID")),
new("ContinuityWarnings", "ContinuityWarningID", new Link("BookID", "Books", "BookID"), new Link("ChapterID", "Chapters", "ChapterID"), new Link("ProjectID", "Project", "ProjectID"), new Link("SceneID", "Scenes", "SceneID"), new Link("ValidationRunID", "ValidationRuns", "ValidationRunID")),
new("FloorPlanBlocks", "FloorPlanBlockID", new Link("FloorPlanFloorID", "FloorPlanFloors", "FloorPlanFloorID"), new Link("LocationID", "Locations", "LocationID")),
new("FloorPlanFloors", "FloorPlanFloorID", new Link("FloorPlanID", "FloorPlans", "FloorPlanID"), new Link("LocationID", "Locations", "LocationID")),
new("FloorPlanTransitions", "FloorPlanTransitionID", new Link("FloorPlanFloorID", "FloorPlanFloors", "FloorPlanFloorID"), new Link("FromLocationID", "Locations", "LocationID"), new Link("ToLocationID", "Locations", "LocationID")),
new("FloorPlans", "FloorPlanID", new Link("LocationID", "Locations", "LocationID"), new Link("ProjectID", "Project", "ProjectID")),
new("GenreMetricPresetMetrics", null, new Link("GenreMetricPresetID", "GenreMetricPresets", "GenreMetricPresetID"), new Link("MetricTypeID", "SceneMetricTypes", "MetricTypeID")),
new("GenreMetricPresets", "GenreMetricPresetID"),
new("LocationAliases", "LocationAliasID", new Link("LocationID", "Locations", "LocationID")),
new("LocationRelationships", "LocationRelationshipID", new Link("FromLocationID", "Locations", "LocationID"), new Link("ToLocationID", "Locations", "LocationID")),
new("Locations", "LocationID", new Link("ParentLocationID", "Locations", "LocationID"), new Link("ProjectID", "Project", "ProjectID")),
new("ManuscriptDocuments", "ManuscriptDocumentID", new Link("BookID", "Books", "BookID"), new Link("ProjectID", "Project", "ProjectID")),
new("ManuscriptStructuralChapterSources", null, new Link("BookID", "Books", "BookID"), new Link("ChapterID", "Chapters", "ChapterID"), new Link("ProjectID", "Project", "ProjectID")),
new("MapFeatures", null, new Link("FloorPlanID", "FloorPlans", "FloorPlanID"), new Link("LocationID", "Locations", "LocationID"), new Link("MapID", "StoryMaps", "MapID")),
new("MapJourneyNotes", null, new Link("CharacterID", "Characters", "CharacterID"), new Link("FromSceneID", "Scenes", "SceneID"), new Link("MapID", "StoryMaps", "MapID"), new Link("ToSceneID", "Scenes", "SceneID")),
new("MapReferenceImages", null, new Link("ProjectID", "Project", "ProjectID")),
new("MapRelationships", null, new Link("MapID", "MapFeatures", "MapID"), new Link("MapID", "StoryMaps", "MapID"), new Link("SourceID", "MapFeatures", "FeatureID"), new Link("TargetID", "MapFeatures", "FeatureID")),
new("PlotEventTargetPlotLines", null, new Link("PlotEventID", "ThreadEvents", "ThreadEventID"), new Link("PlotLineID", "PlotLines", "PlotLineID")),
new("PlotLines", "PlotLineID", new Link("BookID", "Books", "BookID"), new Link("EmergesFromPlotLineID", "PlotLines", "PlotLineID"), new Link("ParentPlotLineID", "PlotLines", "PlotLineID"), new Link("ProjectID", "Project", "ProjectID")),
new("PlotThreads", "PlotThreadID", new Link("ActualResolutionSceneID", "Scenes", "SceneID"), new Link("IntroducedSceneID", "Scenes", "SceneID"), new Link("PlannedResolutionSceneID", "Scenes", "SceneID"), new Link("PlotLineID", "PlotLines", "PlotLineID")),
new("TimelineMetricSettings", null, new Link("MetricTypeID", "SceneMetricTypes", "MetricTypeID"), new Link("ProjectID", "Project", "ProjectID")),
new("TimelineSettings", "ProjectTimelineSettingsID", new Link("ProjectID", "Project", "ProjectID")),
new("ProjectAccess", "ProjectUserAccessID", new Link("ProjectID", "Project", "ProjectID")),
new("RelationshipEvents", "RelationshipEventID", new Link("CharacterAID", "Characters", "CharacterID"), new Link("CharacterBID", "Characters", "CharacterID"), new Link("CharacterRelationshipID", "CharacterRelationships", "CharacterRelationshipID"), new Link("SceneID", "Scenes", "SceneID")),
new("ScenarioSceneOrder", "ScenarioSceneOrderID", new Link("ProposedChapterID", "Chapters", "ChapterID"), new Link("ScenarioID", "Scenarios", "ScenarioID"), new Link("SceneID", "Scenes", "SceneID")),
new("ScenarioValidationRuns", "ScenarioValidationRunID", new Link("ScenarioID", "Scenarios", "ScenarioID")),
new("ScenarioWarnings", "ScenarioWarningID", new Link("ScenarioID", "Scenarios", "ScenarioID"), new Link("ScenarioValidationRunID", "ScenarioValidationRuns", "ScenarioValidationRunID"), new Link("SceneID", "Scenes", "SceneID")),
new("Scenarios", "ScenarioID", new Link("BookID", "Books", "BookID"), new Link("ProjectID", "Project", "ProjectID")),
new("SceneAssetLocations", "SceneAssetLocationID", new Link("LocationID", "Locations", "LocationID"), new Link("SceneID", "Scenes", "SceneID"), new Link("StoryAssetID", "StoryAssets", "StoryAssetID")),
new("SceneAttachments", "SceneAttachmentID", new Link("SceneID", "Scenes", "SceneID")),
new("SceneCharacters", "SceneCharacterID", new Link("CharacterID", "Characters", "CharacterID"), new Link("EntryLocationID", "Locations", "LocationID"), new Link("ExitLocationID", "Locations", "LocationID"), new Link("LocationID", "Locations", "LocationID"), new Link("SceneID", "Scenes", "SceneID")),
new("SceneChecklistItems", "SceneChecklistItemID", new Link("SceneID", "Scenes", "SceneID")),
new("SceneDependencies", "SceneDependencyID", new Link("SourceSceneID", "Scenes", "SceneID"), new Link("TargetSceneID", "Scenes", "SceneID")),
new("SceneFloorPlanOccupancy", "SceneFloorPlanOccupancyID", new Link("CharacterID", "Characters", "CharacterID"), new Link("FloorPlanFloorID", "FloorPlanFloors", "FloorPlanFloorID"), new Link("FloorPlanID", "FloorPlans", "FloorPlanID"), new Link("LocationID", "Locations", "LocationID"), new Link("SceneID", "Scenes", "SceneID"), new Link("StoryAssetID", "StoryAssets", "StoryAssetID")),
new("SceneManuscriptSources", null, new Link("BookID", "Books", "BookID"), new Link("ChapterID", "Chapters", "ChapterID"), new Link("ProjectID", "Project", "ProjectID"), new Link("SceneID", "Scenes", "SceneID")),
new("SceneMetricTypes", "MetricTypeID", new Link("ProjectID", "Project", "ProjectID")),
new("SceneMetricValues", null, new Link("MetricTypeID", "SceneMetricTypes", "MetricTypeID"), new Link("SceneID", "Scenes", "SceneID")),
new("SceneNotes", "SceneNoteID", new Link("SceneID", "Scenes", "SceneID")),
new("SceneOutcomes", "SceneOutcomeID", new Link("SceneID", "Scenes", "SceneID")),
new("ScenePurposes", null, new Link("SceneID", "Scenes", "SceneID")),
new("SceneWorkflow", "SceneWorkflowID", new Link("SceneID", "Scenes", "SceneID")),
new("Scenes", "SceneID", new Link("ChapterID", "Chapters", "ChapterID"), new Link("FloorPlanID", "FloorPlans", "FloorPlanID"), new Link("InitialFloorPlanFloorID", "FloorPlanFloors", "FloorPlanFloorID"), new Link("ManuscriptDocumentID", "ManuscriptDocuments", "ManuscriptDocumentID"), new Link("POVCharacterID", "Characters", "CharacterID"), new Link("PrimaryLocationID", "Locations", "LocationID")),
new("StoryAssets", "StoryAssetID", new Link("CurrentLocationID", "Locations", "LocationID"), new Link("CurrentStateID", "AssetStates", "AssetStateID"), new Link("ProjectID", "Project", "ProjectID")),
new("StoryMaps", "MapID", new Link("ParentMapID", "StoryMaps", "MapID"), new Link("ProjectID", "Project", "ProjectID")),
new("ThreadEvents", "ThreadEventID", new Link("PlotLineID", "PlotLines", "PlotLineID"), new Link("PlotThreadID", "PlotThreads", "PlotThreadID"), new Link("SceneID", "Scenes", "SceneID"), new Link("TargetPlotLineID", "PlotLines", "PlotLineID")),
new("TimelineViewPresets", "TimelineViewPresetID", new Link("BookID", "Books", "BookID"), new Link("ProjectID", "Project", "ProjectID")),
new("UserFiles", "UserFileID", new Link("ProjectID", "Project", "ProjectID")),
new("ValidationRuns", "ValidationRunID", new Link("BookID", "Books", "BookID"), new Link("ProjectID", "Project", "ProjectID")),
new("WritingPlans", "WritingPlanID", new Link("BookID", "Books", "BookID"), new Link("ProjectID", "Project", "ProjectID")),
new("WritingScheduleItems", "WritingScheduleItemID", new Link("SceneID", "Scenes", "SceneID"), new Link("WritingPlanID", "WritingPlans", "WritingPlanID"))
];
public static string? Validate(ProjectBackupExport backup,bool portable=false)
{
if(backup.Project is null || backup.Project.ProjectID<=0) return "Project backup contains an invalid source project identifier.";
var rows=Entities.ToDictionary(entity=>entity.Collection,entity=>((IEnumerable)typeof(ProjectBackupData).GetProperty(entity.Collection)!.GetValue(backup.Data)!).Cast<object>().ToArray());
object? Value(object row,string column)=>row.GetType().GetProperty(column)?.GetValue(row);
foreach(var entity in Entities)
{
if(entity.Identity is not null)
{
var ids=rows[entity.Collection].Select(row=>Value(row,entity.Identity)).ToArray();
if(ids.Any(id=>id is null || id is int integer && integer<=0 || id is long wide && wide<=0) || ids.Distinct().Count()!=ids.Length)
return $"Project backup contains duplicate or missing {entity.Collection} identifiers.";
}
foreach(var link in entity.Links)
{
// These older formats did not carry document rows; copied links are detached on import.
if(link.TargetCollection=="CharacterImages" && backup.FormatVersion!="1.3")continue;
if(link.TargetCollection=="ManuscriptDocuments" && backup.FormatVersion is "1.0" or "1.1") continue;
var allowed=link.TargetCollection=="Project" ? new HashSet<object?> { backup.Project.ProjectID }
: rows[link.TargetCollection].Select(row=>Value(row,link.TargetColumn)).Where(value=>value is not null).ToHashSet();
if(rows[entity.Collection].Any(row=>Value(row,link.Column) is object value && !allowed.Contains(value)&&!(portable&&entity.Identity!=null&&Value(row,entity.Identity) is int owner&&backup.Recovery.ExternalReferences.Any(r=>r.BindingRequired&&r.Collection==entity.Collection&&r.OwnerID==owner&&r.Field==link.Column&&r.TargetKey.Split('/').Length==3&&r.TargetKey.Split('/')[1]==link.TargetCollection))))
return $"Project backup {entity.Collection}.{link.Column} references a missing or foreign {link.TargetCollection} record.";
}
}
if(backup.Data.StoryIntelligenceRuns.Count>0 && backup.Data.Scenes.Any(scene=>scene.ImportRunID.HasValue && !backup.Data.StoryIntelligenceRuns.Any(run=>run.StoryIntelligenceRunID==scene.ImportRunID)))
return "Project backup scene analysis provenance references a missing or foreign run.";
return ProjectBackupNarrative.Validate(backup) ?? ProjectBackupEntityReview.Validate(backup) ?? ProjectBackupStoryMemory.Validate(backup) ?? ProjectBackupTimelineState.Validate(backup);
}
}