using PlotLine.Data; using Microsoft.AspNetCore.Mvc.Rendering; using PlotLine.Models; using PlotLine.ViewModels; namespace PlotLine.Services; // Timeline's metric rows include SQL-filled defaults. Only recorded SceneMetricValues are evidence. public static class WritingContextMetrics { public static TimelineViewModel Build(TimelineData data,IReadOnlyList recorded) { var scenes=StoryStateService.OrderScenes(data).ToArray();var byId=scenes.ToDictionary(s=>s.SceneID); return new(){Project=data.Project!,OrderedScenes=scenes,ChapterOptions=data.Chapters.Select(c=>new SelectListItem($"{c.ChapterNumber:0.##} ยท {c.ChapterTitle}",c.ChapterID.ToString())).ToArray(),MetricGraphs=recorded.OrderBy(p=>p.SortOrder).ThenBy(p=>p.MetricName).Where(p=>byId.ContainsKey(p.SceneID)).GroupBy(p=>p.MetricTypeID).Select(group=>{ var metric=group.First();return new MetricGraphViewModel{MetricTypeID=group.Key,MetricName=metric.MetricName,MinValue=metric.MinValue,MaxValue=metric.MaxValue, Points=group.Select(p=>new MetricGraphPointViewModel{SceneID=p.SceneID,SceneNumber=byId[p.SceneID].SceneNumber,SceneTitle=byId[p.SceneID].SceneTitle,Value=p.Value}).ToArray()}; }).ToArray()}; } }