@model ChapterWorkflowViewModel
@{
    ViewData["Title"] = $"Chapter Workflow - {Model.Chapter.ChapterTitle}";
    var blockedCount = Model.Scenes.Count(x => x.IsBlocked);
    var readyDraftCount = Model.Scenes.Count(x => x.ReadyForDraft);
    var readyRevisionCount = Model.Scenes.Count(x => x.ReadyForRevision);
    var readyPolishCount = Model.Scenes.Count(x => x.ReadyForPolish);
}

<nav class="breadcrumb-trail" aria-label="Breadcrumb">
    <a asp-controller="Projects" asp-action="Index">Projects</a>
    <a asp-controller="Projects" asp-action="Details" asp-route-id="@Model.Project.ProjectID">@Model.Project.ProjectName</a>
    <a asp-controller="Books" asp-action="Details" asp-route-id="@Model.Book.BookID">@Model.Book.BookTitle</a>
    <a asp-controller="Chapters" asp-action="Details" asp-route-id="@Model.Chapter.ChapterID">Chapter @Model.Chapter.ChapterNumber</a>
    <span>Workflow</span>
</nav>

<div class="page-heading">
    <div>
        <p class="eyebrow">Chapter workflow</p>
        <h1>Chapter @Model.Chapter.ChapterNumber: @Model.Chapter.ChapterTitle</h1>
        <p class="lead-text">Chapter intent, readiness, blockers, notes, and checklist progress.</p>
    </div>
    <div class="button-row">
        <a class="btn btn-outline-primary" asp-controller="Timeline" asp-action="Index" asp-route-projectId="@Model.Project.ProjectID" asp-route-bookId="@Model.Book.BookID">Open timeline</a>
        <a class="btn btn-outline-primary" asp-controller="Exports" asp-action="ChapterBrief" asp-route-id="@Model.Chapter.ChapterID">Export brief</a>
        <a class="btn btn-outline-secondary" asp-controller="Exports" asp-action="ChapterBrief" asp-route-id="@Model.Chapter.ChapterID" asp-route-format="markdown">Markdown</a>
        <a class="btn btn-outline-secondary" asp-controller="Warnings" asp-action="Index" asp-route-projectId="@Model.Project.ProjectID" asp-route-bookId="@Model.Book.BookID">Warnings</a>
    </div>
</div>

<section class="story-bible-section">
    <div class="story-bible-grid story-bible-grid-compact">
        <article class="story-bible-card"><h3>Ready to draft</h3><strong class="writer-kpi">@readyDraftCount</strong></article>
        <article class="story-bible-card"><h3>Ready to revise</h3><strong class="writer-kpi">@readyRevisionCount</strong></article>
        <article class="story-bible-card"><h3>Ready to polish</h3><strong class="writer-kpi">@readyPolishCount</strong></article>
        <article class="story-bible-card"><h3>Blocked</h3><strong class="writer-kpi">@blockedCount</strong></article>
    </div>
</section>

<section class="story-bible-section">
    <div class="story-bible-section-heading">
        <div>
            <p class="eyebrow">Intent</p>
            <h2>Chapter Goals</h2>
        </div>
    </div>
    <form asp-action="SaveChapterGoal" method="post" class="story-bible-card chapter-goal-form">
        <input type="hidden" name="ChapterGoalID" value="@Model.Goal.ChapterGoalID" />
        <input type="hidden" name="ChapterID" value="@Model.Chapter.ChapterID" />
        <label class="form-label">Goal summary</label>
        <textarea class="form-control" name="GoalSummary" rows="3">@Model.Goal.GoalSummary</textarea>
        <label class="form-label">Emotional goal</label>
        <input class="form-control" name="EmotionalGoal" value="@Model.Goal.EmotionalGoal" />
        <label class="form-label">Reader takeaway</label>
        <textarea class="form-control" name="ReaderTakeaway" rows="2">@Model.Goal.ReaderTakeaway</textarea>
        <label class="form-label">Must include</label>
        <textarea class="form-control" name="MustInclude" rows="3">@Model.Goal.MustInclude</textarea>
        <label class="form-label">Risks or concerns</label>
        <textarea class="form-control" name="RisksOrConcerns" rows="3">@Model.Goal.RisksOrConcerns</textarea>
        <button class="btn btn-primary mt-2" type="submit">Save chapter goals</button>
    </form>
</section>

<section class="story-bible-section">
    <div class="story-bible-section-heading">
        <div>
            <p class="eyebrow">Scenes</p>
            <h2>Workflow Summary</h2>
        </div>
    </div>
    <div class="writer-queue-list">
        @foreach (var scene in Model.Scenes)
        {
            <article class="writer-queue-card">
                <div class="writer-queue-title">
                    <h3>Scene @scene.SceneNumber: @scene.SceneTitle</h3>
                    <span class="status-pill">@scene.DraftStatus</span>
                </div>
                <div class="story-bible-meta">
                    @if (scene.Priority.HasValue) { <span>Priority @scene.Priority</span> }
                    <span>@scene.WarningCount warnings</span>
                    <span>@scene.RemainingChecklistCount checklist remaining</span>
                    <span>@scene.UnresolvedNoteCount open notes</span>
                    @if (scene.ReadyForDraft) { <span>Ready for draft</span> }
                    @if (scene.ReadyForRevision) { <span>Ready for revision</span> }
                    @if (scene.ReadyForPolish) { <span>Ready for polish</span> }
                    @if (scene.IsBlocked) { <span>Blocked</span> }
                </div>
                @if (!string.IsNullOrWhiteSpace(scene.BlockedReason))
                {
                    <p class="muted">@scene.BlockedReason</p>
                }
                <div class="story-bible-actions">
                    <a asp-controller="Scenes" asp-action="Edit" asp-route-id="@scene.SceneID">Open scene</a>
                    <a asp-controller="Timeline" asp-action="Index" asp-route-projectId="@Model.Project.ProjectID" asp-route-bookId="@Model.Book.BookID" asp-route-selectedSceneId="@scene.SceneID">Timeline</a>
                    <a asp-controller="Warnings" asp-action="Index" asp-route-projectId="@Model.Project.ProjectID" asp-route-bookId="@Model.Book.BookID" asp-route-sceneId="@scene.SceneID">Warnings</a>
                </div>
            </article>
        }
    </div>
</section>
