using System.Data;using System.Text.Json;using Dapper;using PlotLine.Models;using PlotLine.Services; namespace PlotLine.Data; public sealed record ContextLensRead(RelationshipMapData Data,int? FocusCharacterID,bool Truncated); public interface IContextRelationshipReader { Task Read(int project,int user,int scene,int? focus,string lens,int depth,IReadOnlyList physical,CancellationToken cancellationToken=default); Task> Lookup(int project,int user,string search,CancellationToken cancellationToken=default); } public sealed class ContextRelationshipReader(ISqlConnectionFactory connectionFactory,IConfiguration configuration):IContextRelationshipReader { private sealed record Boundary(int? FocusCharacterID,bool Truncated); public async Task Read(int project,int user,int scene,int? focus,string lens,int depth,IReadOnlyList physical,CancellationToken cancellationToken=default) { using var db=connectionFactory.CreateConnection();IReadOnlyList books;IReadOnlyList chapters;IReadOnlyList scenes;IReadOnlyList people;IReadOnlyList links;Boundary boundary; using(var grid=await db.QueryMultipleAsync(new CommandDefinition("dbo.ContextRelationshipLens_Read",new{ProjectID=project,UserID=user,FocusCharacterID=focus,Lens=lens,Depth=depth,PhysicalIDs=JsonSerializer.Serialize(physical)},commandType:CommandType.StoredProcedure,cancellationToken:cancellationToken))){books=(await grid.ReadAsync()).ToList();chapters=(await grid.ReadAsync()).ToList();scenes=(await grid.ReadAsync()).ToList();people=(await grid.ReadAsync()).ToList();links=(await grid.ReadAsync()).ToList();boundary=await grid.ReadSingleAsync();} if(books.Count>20000||chapters.Count>20000||scenes.Count>20000)throw new ArgumentException("This project exceeds the Context narrative-order limit. Use the full Relationships Editor."); var order=StoryStateService.OrderScenes(new(){Books=books,Chapters=chapters,Scenes=scenes}).Select((s,i)=>new{s.SceneID,Ordinal=i}).ToList();var at=lens=="family"||scene==0?order.Count-1:order.FirstOrDefault(s=>s.SceneID==scene)?.Ordinal??-1; var changes=(await db.QueryAsync(new CommandDefinition("dbo.ContextRelationshipLens_Events",new{ProjectID=project,UserID=user,RelationshipIDs=JsonSerializer.Serialize(links.Select(r=>r.CharacterRelationshipID)),SceneOrder=JsonSerializer.Serialize(order),AtOrdinal=at},commandType:CommandType.StoredProcedure,cancellationToken:cancellationToken))).ToList(); await ParagraphEvidenceProjection.Apply(db,configuration,changes); return new(new(){Books=books,Chapters=chapters,Scenes=scenes,Characters=people,Relationships=links,RelationshipEvents=changes},boundary.FocusCharacterID,boundary.Truncated); } public async Task> Lookup(int project,int user,string search,CancellationToken cancellationToken=default) {using var db=connectionFactory.CreateConnection();return (await db.QueryAsync(new CommandDefinition("dbo.ContextCharacter_Lookup",new{ProjectID=project,UserID=user,Search=search},commandType:CommandType.StoredProcedure,cancellationToken:cancellationToken))).ToList();} }