using System.Globalization; using System.Text.Json; namespace PlotLine.Models; public sealed record AuthoringField(string Name,string Label,string Type="text",bool Required=false,int Max=int.MaxValue); public sealed class AuthoringSection { public string Kind {get;set;}=""; public int EntityID {get;set;} public string Data {get;set;}="{}";public string Version {get;set;}=""; public int BookID {get;set;} public int ChapterID {get;set;} public int? SceneID {get;set;} public IReadOnlyList<(string Value,string Label)> Purposes {get;set;}=[]; public IReadOnlyList<(string Value,string Label)> NoteTypes {get;set;}=[]; public IReadOnlyList Fields=>AuthoringSchema.Fields(Kind); public string Title=>Kind switch{"Chapter"=>"Chapter purpose and summary","Goals"=>"What should this chapter accomplish?","Scene"=>"What needs to happen in this scene?","Workflow"=>"Working details and blockers","Note"=>"Scene note","AddNote"=>"Add a scene note","Checklist"=>"Reminder","AddChecklist"=>"Add something to remember",_=>Kind}; public string Value(string key){if(Kind.StartsWith("Add",StringComparison.Ordinal))return key=="SceneNoteTypeID"?NoteTypes.FirstOrDefault().Value??"":"";using var json=JsonDocument.Parse(Data);if(json.RootElement.ValueKind!=JsonValueKind.Object||!json.RootElement.TryGetProperty(key,out var v)||v.ValueKind==JsonValueKind.Null)return "";return v.ValueKind==JsonValueKind.True?"true":v.ValueKind==JsonValueKind.False?"false":v.ToString();} public string Key=>$"{Kind}-{EntityID}"; } public static class AuthoringSchema { public static IReadOnlyList Fields(string kind)=>kind switch{ "Chapter"=>[new("ChapterPurposeID","Optional chapter purpose","purpose"),new("Summary","Chapter summary","textarea")], "Goals"=>[new("GoalSummary","What would you like this chapter to accomplish?","textarea"),new("EmotionalGoal","How should the reader feel?","textarea"),new("ReaderTakeaway","What should the reader take away?","textarea"),new("MustInclude","Things to include or remember","textarea"),new("RisksOrConcerns","Concerns to revisit","textarea")], "Scene"=>[new("ScenePurposeNotes","What needs to happen in this scene?","textarea"),new("SceneOutcomeNotes","What should change by the end?","textarea")], "Note" or "AddNote"=>[new("SceneNoteTypeID","Note type","noteType",true),new("NoteTitle","Optional title","text",false,200),new("NoteText","What do you need to remember?","textarea",true),new("IsPinned","Keep prominent","checkbox"),new("IsResolved","Resolved","checkbox"),new("SortOrder","Optional order","number")], "Checklist" or "AddChecklist"=>[new("ItemText","Something to remember","text",true,300),new("IsCompleted","Done","checkbox"),new("SortOrder","Optional order","number")], "Workflow"=>[new("Priority","Priority (1 to 10)","number"),new("EstimatedWordCount","Estimated words","number"),new("ActualWordCount","Recorded working word count","number"),new("DraftedDate","Drafted on","date"),new("LastWorkedOn","Last worked on","date"),new("IsBlocked","Something is blocking this scene","checkbox"),new("BlockedReason","What is in the way?","textarea")],_=>[]}; public static string? Validate(string kind,IReadOnlyDictionary values){var fields=Fields(kind);if(fields.Count==0)return "This editor is unavailable.";foreach(var f in fields){var v=values.GetValueOrDefault(f.Name)??"";if(f.Required&&string.IsNullOrWhiteSpace(v))return $"Enter {f.Label.ToLowerInvariant()}.";if(v.Length>f.Max)return $"{f.Label} allows up to {f.Max} characters.";if(v.Length>0&&f.Type is "number" or "purpose" or "noteType"&&!int.TryParse(v,NumberStyles.Integer,CultureInfo.InvariantCulture,out _))return $"Choose a valid value for {f.Label.ToLowerInvariant()}.";if(v.Length>0&&f.Type=="date"&&!DateOnly.TryParseExact(v,"yyyy-MM-dd",CultureInfo.InvariantCulture,DateTimeStyles.None,out _))return "Use a valid calendar date.";if(v.Length>0&&f.Type=="checkbox"&&v is not("true" or "false"))return "Choose a valid checkbox value.";} if(kind=="Workflow"){foreach(var name in new[]{"EstimatedWordCount","ActualWordCount"})if(int.TryParse(values.GetValueOrDefault(name),out var n)&&n<0)return "Word counts must be non-negative.";if(int.TryParse(values.GetValueOrDefault("Priority"),out var priority)&&(priority<1||priority>10))return "Priority must be 1 to 10.";}return null;} } public sealed class WriterAuthoringPage { public int BookID {get;set;} public int ChapterID {get;set;} public int? SceneID {get;set;} public Chapter Chapter {get;set;}=new();public Scene? Scene {get;set;} public IReadOnlyList Scenes {get;set;}=[];public IReadOnlyList Sections {get;set;}=[]; public IReadOnlyList Characters {get;set;}=[];public IReadOnlyList Threads {get;set;}=[];public IReadOnlyList Relationships {get;set;}=[]; public IReadOnlyList Attachments {get;set;}=[]; }