using System.Text.Json.Nodes; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using SoroushAsadi.Services; namespace SoroushAsadi.Pages.Admin.Posts; [Authorize] public class PostEditModel(ContentService content) : PageModel { [BindProperty(SupportsGet = true)] public string Slug { get; set; } = ""; public string CurrentBody { get; private set; } = ""; public string Message { get; private set; } = ""; public bool HasOverride { get; private set; } // Known default bodies live in Blog/Post.cshtml.cs (DefaultBodies) private static readonly Dictionary _defaults = new() { ["rag-eval-framework"] = SoroushAsadi.Pages.Blog.DefaultBodies.RagEval, ["agentic-n8n-patterns"] = SoroushAsadi.Pages.Blog.DefaultBodies.N8nPatterns, ["vertex-cost-control"] = SoroushAsadi.Pages.Blog.DefaultBodies.VertexCost, ["k8s-llm-inference"] = SoroushAsadi.Pages.Blog.DefaultBodies.K8sInference, ["flutter-on-device-ai"] = SoroushAsadi.Pages.Blog.DefaultBodies.FlutterAI, ["enterprise-ai-roadmap"] = SoroushAsadi.Pages.Blog.DefaultBodies.EnterpriseRoadmap, }; public void OnGet() { var overrides = content.GetPostOverrides(); if (overrides.TryGetValue(Slug, out var node) && node["body"]?.GetValue() is { } body) { CurrentBody = body; HasOverride = true; } else { CurrentBody = _defaults.GetValueOrDefault(Slug, ""); } } public IActionResult OnPost(string body, string? reset) { if (reset == "1") { // Remove override — default body shows through var existing = content.GetPostOverrides(); existing.Remove(Slug); // Rebuild the posts JSON without this slug var obj = new JsonObject(); foreach (var kv in existing) obj[kv.Key] = kv.Value.DeepClone(); content.SaveSection(ContentService.PostsKey, obj.ToJsonString()); Message = "Reset to default."; HasOverride = false; CurrentBody = _defaults.GetValueOrDefault(Slug, ""); return Page(); } content.SavePost(Slug, new JsonObject { ["body"] = body }); HasOverride = true; CurrentBody = body; Message = "Saved."; return Page(); } } // Re-export DefaultBodies from the Blog page so this page can use them