using Dapper; using PlotLine.Models; namespace PlotLine.Data; public interface IStoryMemoryRepository { Task ClearDerivedStateAsync(int importSessionId); Task> ListProcessedSceneResultIdsAsync(int importSessionId, string processorVersion); Task MarkSceneResultProcessedAsync(int importSessionId, int sceneResultId, string processorVersion, string snapshotHash); Task UpsertCharacterAsync(StoryMemoryCharacterSave request); Task AddCharacterAliasAsync(int characterId, string alias, string normalisedAlias, string reason, int sceneResultId, decimal confidence); Task AddCharacterAttributeAsync(int characterId, string attributeType, string value, decimal confidence, bool isExplicit, string evidenceSummary, int sceneResultId); Task UpsertLocationAsync(StoryMemoryLocationSave request); Task UpsertAssetAsync(StoryMemoryAssetSave request); Task UpsertRelationshipAsync(int importSessionId, string sourceEntityType, int sourceEntityId, string targetEntityType, int targetEntityId, string relationshipType, decimal confidence, bool isExplicit, int sceneResultId); Task> ListCharactersAsync(int importSessionId); Task> ListCharacterAttributesAsync(int importSessionId); Task> ListLocationsAsync(int importSessionId); Task> ListAssetsAsync(int importSessionId); Task> ListAssignmentsAsync(int importSessionId); Task GetAssignmentAsync(int importSessionId, string entityType, int entityId); Task UpsertAssignmentAsync(StoryMemoryIllustrationAssignmentSave request); Task InvalidateAssignmentAsync(int assignmentId, string reason); Task UpsertDemandAsync(StoryMemoryIllustrationDemandSave request); Task> ListDemandsAsync(int importSessionId); Task GetAppearancePreferenceAsync(int importSessionId); Task GetImportContextAsync(int importSessionId); Task SetAppearancePreferenceAsync(int importSessionId, string preference); Task SetImportContextAsync(int importSessionId, string appearancePreference, string? primaryAgeGroup, string? storyEra, string? storyLocation); Task GetDiagnosticsAsync(int importSessionId); } public sealed class StoryMemoryRepository(ISqlConnectionFactory connectionFactory) : IStoryMemoryRepository { public async Task ClearDerivedStateAsync(int importSessionId) { using var connection = connectionFactory.CreateConnection(); await connection.ExecuteAsync( "dbo.StoryMemory_ClearDerivedState", new { ImportSessionID = importSessionId },commandType:System.Data.CommandType.StoredProcedure); } public async Task> ListProcessedSceneResultIdsAsync(int importSessionId, string processorVersion) { using var connection = connectionFactory.CreateConnection(); var ids = await connection.QueryAsync( "dbo.StoryMemory_ListProcessedSceneResultIds", new { ImportSessionID = importSessionId, ProcessorVersion = processorVersion }, commandType: System.Data.CommandType.StoredProcedure); return ids.ToHashSet(); } public async Task MarkSceneResultProcessedAsync(int importSessionId, int sceneResultId, string processorVersion, string snapshotHash) { using var connection = connectionFactory.CreateConnection(); await connection.ExecuteAsync( "dbo.StoryMemory_MarkSceneResultProcessed", new { ImportSessionID = importSessionId, SceneResultID = sceneResultId, ProcessorVersion = processorVersion, SnapshotHash = snapshotHash },commandType:System.Data.CommandType.StoredProcedure); } public async Task UpsertCharacterAsync(StoryMemoryCharacterSave request) { using var connection = connectionFactory.CreateConnection(); return await connection.QuerySingleAsync( "dbo.StoryMemory_UpsertCharacter", request,commandType:System.Data.CommandType.StoredProcedure); } public async Task AddCharacterAliasAsync(int characterId, string alias, string normalisedAlias, string reason, int sceneResultId, decimal confidence) { using var connection = connectionFactory.CreateConnection(); await connection.ExecuteAsync( "dbo.StoryMemory_AddCharacterAlias", new { StoryMemoryCharacterID = characterId, Alias = alias, NormalisedAlias = normalisedAlias, ResolutionReason = reason, SourceSceneResultID = sceneResultId, Confidence = confidence },commandType:System.Data.CommandType.StoredProcedure); } public async Task AddCharacterAttributeAsync(int characterId, string attributeType, string value, decimal confidence, bool isExplicit, string evidenceSummary, int sceneResultId) { using var connection = connectionFactory.CreateConnection(); await connection.ExecuteAsync( "dbo.StoryMemory_AddCharacterAttribute", new { StoryMemoryCharacterID = characterId, AttributeType = attributeType, NormalisedValue = value, Confidence = confidence, IsExplicit = isExplicit, EvidenceSummary = evidenceSummary, SourceSceneResultID = sceneResultId },commandType:System.Data.CommandType.StoredProcedure); } public async Task UpsertLocationAsync(StoryMemoryLocationSave request) { using var connection = connectionFactory.CreateConnection(); return await connection.QuerySingleAsync( "dbo.StoryMemory_UpsertLocation", request,commandType:System.Data.CommandType.StoredProcedure); } public async Task UpsertAssetAsync(StoryMemoryAssetSave request) { using var connection = connectionFactory.CreateConnection(); return await connection.QuerySingleAsync( "dbo.StoryMemory_UpsertAsset", request,commandType:System.Data.CommandType.StoredProcedure); } public async Task UpsertRelationshipAsync(int importSessionId, string sourceEntityType, int sourceEntityId, string targetEntityType, int targetEntityId, string relationshipType, decimal confidence, bool isExplicit, int sceneResultId) { using var connection = connectionFactory.CreateConnection(); await connection.ExecuteAsync( "dbo.StoryMemory_UpsertRelationship", new { ImportSessionID = importSessionId, SourceEntityType = sourceEntityType, SourceEntityID = sourceEntityId, TargetEntityType = targetEntityType, TargetEntityID = targetEntityId, RelationshipType = relationshipType, Confidence = confidence, IsExplicit = isExplicit, SceneResultID = sceneResultId },commandType:System.Data.CommandType.StoredProcedure); } public async Task> ListCharactersAsync(int importSessionId) { using var connection = connectionFactory.CreateConnection(); return (await connection.QueryAsync("dbo.StoryMemory_ListCharacters", new { ImportSessionID = importSessionId }, commandType: System.Data.CommandType.StoredProcedure)).ToList(); } public async Task> ListCharacterAttributesAsync(int importSessionId) { using var connection = connectionFactory.CreateConnection(); return (await connection.QueryAsync( "dbo.StoryMemory_ListCharacterAttributes", new { ImportSessionID = importSessionId }, commandType: System.Data.CommandType.StoredProcedure)).ToList(); } public async Task> ListLocationsAsync(int importSessionId) { using var connection = connectionFactory.CreateConnection(); return (await connection.QueryAsync("dbo.StoryMemory_ListLocations", new { ImportSessionID = importSessionId }, commandType: System.Data.CommandType.StoredProcedure)).ToList(); } public async Task> ListAssetsAsync(int importSessionId) { using var connection = connectionFactory.CreateConnection(); return (await connection.QueryAsync("dbo.StoryMemory_ListAssets", new { ImportSessionID = importSessionId }, commandType: System.Data.CommandType.StoredProcedure)).ToList(); } public async Task> ListAssignmentsAsync(int importSessionId) { using var connection = connectionFactory.CreateConnection(); return (await connection.QueryAsync("dbo.StoryMemory_ListAssignments", new { ImportSessionID = importSessionId }, commandType: System.Data.CommandType.StoredProcedure)).ToList(); } public async Task GetAssignmentAsync(int importSessionId, string entityType, int entityId) { using var connection = connectionFactory.CreateConnection(); return await connection.QuerySingleOrDefaultAsync( "dbo.StoryMemory_GetAssignment", new { ImportSessionID = importSessionId, EntityType = entityType, EntityID = entityId }, commandType: System.Data.CommandType.StoredProcedure); } public async Task UpsertAssignmentAsync(StoryMemoryIllustrationAssignmentSave request) { using var connection = connectionFactory.CreateConnection(); await connection.ExecuteAsync( "dbo.StoryMemory_UpsertAssignment", request,commandType:System.Data.CommandType.StoredProcedure); } public async Task InvalidateAssignmentAsync(int assignmentId, string reason) { using var connection = connectionFactory.CreateConnection(); await connection.ExecuteAsync( "dbo.StoryMemory_InvalidateAssignment", new { AssignmentID = assignmentId, Reason = reason }, commandType: System.Data.CommandType.StoredProcedure); } public async Task UpsertDemandAsync(StoryMemoryIllustrationDemandSave request) { using var connection = connectionFactory.CreateConnection(); return await connection.QuerySingleAsync( "dbo.StoryMemory_UpsertDemand", request,commandType:System.Data.CommandType.StoredProcedure); } public async Task> ListDemandsAsync(int importSessionId) { using var connection = connectionFactory.CreateConnection(); return (await connection.QueryAsync( "dbo.StoryMemory_ListDemands", new { ImportSessionID = importSessionId }, commandType: System.Data.CommandType.StoredProcedure)).ToList(); } public async Task GetAppearancePreferenceAsync(int importSessionId) => (await GetImportContextAsync(importSessionId)).AppearancePreference; public async Task GetImportContextAsync(int importSessionId) { using var connection = connectionFactory.CreateConnection(); return await connection.QuerySingleOrDefaultAsync( "dbo.StoryMemory_GetImportContext", new { ImportSessionID = importSessionId }, commandType: System.Data.CommandType.StoredProcedure) ?? new StoryMemoryImportContext(StoryMemoryAppearancePreferences.Balanced, string.Empty, string.Empty, string.Empty); } public async Task SetAppearancePreferenceAsync(int importSessionId, string preference) => await SetImportContextAsync(importSessionId, preference, null, null, null); public async Task SetImportContextAsync(int importSessionId, string appearancePreference, string? primaryAgeGroup, string? storyEra, string? storyLocation) { using var connection = connectionFactory.CreateConnection(); await connection.ExecuteAsync( "dbo.StoryMemory_SetImportContext", new { ImportSessionID = importSessionId, AppearancePreference = appearancePreference, PrimaryAgeGroup = TrimContext(primaryAgeGroup, 120), StoryEra = TrimContext(storyEra, 120), StoryLocation = TrimContext(storyLocation, 160) },commandType:System.Data.CommandType.StoredProcedure); } private static string? TrimContext(string? value, int maxLength) { var clean = value?.Trim(); return string.IsNullOrWhiteSpace(clean) ? null : clean[..Math.Min(clean.Length, maxLength)]; } public async Task GetDiagnosticsAsync(int importSessionId) { using var connection = connectionFactory.CreateConnection(); return await connection.QuerySingleAsync( "dbo.StoryMemory_GetDiagnostics", new { ImportSessionID = importSessionId }, commandType: System.Data.CommandType.StoredProcedure); } }