using System.Security.Claims; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.ViewEngines; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using PlotLine.Controllers; using PlotLine.Data; using PlotLine.Models; using PlotLine.Services; using PlotLine.ViewModels; // Isolated, loopback-only host: renders real compiled Razor chrome, never starts app workers or connects to a database. internal static class ToolbarBrowserHost { public static async Task RunAsync() { var builder = WebApplication.CreateBuilder(new WebApplicationOptions { ApplicationName = typeof(ToolbarController).Assembly.FullName }); builder.Services.AddControllersWithViews().AddApplicationPart(typeof(ToolbarController).Assembly); builder.Services.AddSingleton(); builder.Services.AddHttpContextAccessor(); builder.Services.AddSingleton(); builder.Services.AddScoped(_ => new ApplicationNavigation(null!, null!, null!)); builder.Services.AddSingleton(); builder.Services.AddScoped(services => new ToolbarTestUser(int.Parse(services.GetRequiredService().HttpContext!.User.FindFirstValue(ClaimTypes.NameIdentifier)!))); var app = builder.Build(); app.Urls.Add("http://127.0.0.1:5238"); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.GetFullPath("PlotLine/wwwroot")) }); app.Use(async (context, next) => { var user = context.Request.Cookies["fixture-user"] == "5" ? "5" : "4"; context.User = new ClaimsPrincipal(new ClaimsIdentity([new Claim(ClaimTypes.NameIdentifier, user), new Claim(ClaimTypes.Name, "Fixture Author")], "Fixture")); await next(); }); app.UseRouting(); app.UseAuthorization(); app.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}"); app.MapGet("/fixture", async context => { var model = new ApplicationNavigationViewModel { ActiveSection = "Calendar", ShowCompanionPresence = true }; if (context.Request.Query["project"] != "none") { model.Project = new Project { ProjectID = 7, ProjectName = "The Glass Harbour", AccessRole = "Owner" }; model.Book = new Book { BookID = 12, ProjectID = 7, BookTitle = "A Distant Shore", BookNumber = 1 }; model.Books = [model.Book, new Book { BookID = 13, ProjectID = 7, BookTitle = "The Return", BookNumber = 2 }]; } var dashboardPage = context.Request.Query["page"] == "dashboard"; if (dashboardPage) model.ActiveSection = "Overview"; var appearance = await context.RequestServices.GetRequiredService().GetAsync(int.Parse(context.User.FindFirstValue(ClaimTypes.NameIdentifier)!)); var route = new RouteData(); route.Values["controller"] = dashboardPage ? "Projects" : "StoryCalendar"; route.Values["action"] = dashboardPage ? "Details" : "Index"; var action = new ActionContext(context, route, new ActionDescriptor()); async Task Render(string name, object? viewModel = null) { var view = context.RequestServices.GetRequiredService().GetView(null, $"/Views/{name}.cshtml", false); if (!view.Success) throw new Exception(string.Join(",", view.SearchedLocations)); using var writer = new StringWriter(); var data = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { Model = viewModel ?? model }; data["ApplicationAppearance"] = appearance; var temp = new TempDataDictionary(context, context.RequestServices.GetRequiredService()); await view.View.RenderAsync(new ViewContext(action, view.View, data, temp, writer, new HtmlHelperOptions())); return writer.ToString(); } var menu = await Render("Shared/_ApplicationShell"); var toolbar = ""; var calendar = new StoryCalendarViewModel { Project = model.Project ?? new(), SelectedBookID = model.Book?.BookID, SelectedMonth = new DateOnly(2026, 10, 1), EarliestStoryDate = new DateOnly(2026, 10, 3), LatestStoryDate = new DateOnly(2026, 10, 3), BookOptions = [new SelectListItem("A Distant Shore", "12"), new SelectListItem("The Return", "13")], MonthOptions = [new SelectListItem("October 2026", "2026-10")], DatedScenes = [new StoryCalendarSceneViewModel { Title = "Arrival", StoryDate = new DateOnly(2026, 10, 3) }], Weeks = Enumerable.Range(0, 5).Select(week => new StoryCalendarWeekViewModel { Days = Enumerable.Range(0, 7).Select(day => new StoryCalendarDayViewModel { Date = new DateOnly(2026, 9, 28).AddDays(week * 7 + day), IsInSelectedMonth = week * 7 + day >= 3 }).ToList() }).ToList() }; var surface = dashboardPage ? "" : await Render("StoryCalendar/_StoryCalendarSurface", calendar); if (dashboardPage) { surface = await Render("Projects/Details", new ProjectDetailViewModel { Project = model.Project ?? new(), Summary = new() { TotalBooks = 2, DraftingBooks = 1, PlanningBooks = 1, TotalWords = 68420, CharacterCount = 18, LocationCount = 12, ActivePlotThreadCount = 7, ContinuityWarningCount = 3 }, SeriesProgress = new() { Percentage = 42, SupportingLine = "68,420 of 160,000 planned words", CalculationLabel = "Word count" }, Books = [new() { BookID = 12, BookNumber = 1, BookTitle = "A Distant Shore", Subtitle = "The Glass Harbour · Book One", Status = "Drafting", CurrentWordCount = 68420, TargetWordCount = 80000, ProgressPercentage = 85.5m, ChapterCount = 24, SceneCount = 72, UpdatedDate = new DateTime(2026,10,28) }], RecentActivity = [new() { ActivityType = "Updated", EntityType = "Scene", EntityName = "An unexpected arrival", Description = "Chapter 12 · A change of direction", TimeLabel = "Today", UserLabel = "Fixture Author", IconLabel = "Scene" }], NeedsAttention = new() { Warnings = [new() { SeverityName = "Review", WarningTypeName = "Chronology", Message = "Check the order of events at the harbour.", LocationLabel = "Book 1 · Chapter 12" }] } }); } var initialTheme = appearance.ThemeKey ?? "warm-light"; var appearanceScript = ""; context.Response.ContentType = "text/html; charset=utf-8"; await context.Response.WriteAsync("
" + menu + toolbar + "
" + surface + "
" + appearanceScript + ""); }); await app.RunAsync(); } }