@model ChapterDetailViewModel
@{
    ViewData["Title"] = Model.Chapter.ChapterTitle;
    ViewData["ProjectSection"] = "Overview";
}

<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>
    <span>Chapter @Model.Chapter.ChapterNumber</span>
</nav>

<partial name="_ProjectSectionNav" model="Model.Project" />

@if (TempData["ArchiveMessage"] is string archiveMessage)
{
    <div class="alert alert-info">@archiveMessage</div>
}

<div class="page-heading">
    <div>
        <p class="eyebrow">
            <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>
        </p>
        <h1>Chapter @Model.Chapter.ChapterNumber: @Model.Chapter.ChapterTitle <help-icon key="chapters.overview" /></h1>
        <span class="status-pill">@(Model.Chapter.IsLinkedToManuscript ? "🔗 Linked" : "📝 Planned")</span>
        @if (!string.IsNullOrWhiteSpace(Model.Chapter.ChapterPurposeName))
        {
            <span class="status-pill">@Model.Chapter.ChapterPurposeName</span>
        }
        @if (!string.IsNullOrWhiteSpace(Model.Chapter.Summary))
        {
            <p class="lead-text">@Model.Chapter.Summary</p>
        }
    </div>
    <div class="button-row">
        <a class="btn btn-outline-secondary" asp-action="Edit" asp-route-id="@Model.Chapter.ChapterID">Edit chapter</a>
        <a class="btn btn-outline-primary" asp-controller="Writer" asp-action="Chapter" asp-route-id="@Model.Chapter.ChapterID">Chapter workflow</a>
        <a class="btn btn-outline-primary" asp-controller="Exports" asp-action="ChapterBrief" asp-route-id="@Model.Chapter.ChapterID">Chapter brief</a>
        <a class="btn btn-primary" asp-controller="Scenes" asp-action="Create" asp-route-chapterId="@Model.Chapter.ChapterID">Add scene</a>
        <div class="dropdown pw-actions">
            <button class="btn pw-actions__toggle dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                Actions
            </button>
            <div class="dropdown-menu dropdown-menu-end">
                <form asp-action="Archive" method="post" class="archive-button-form" data-confirm-message="Archive this chapter?&#10;&#10;This will hide it from active lists and timelines, but the data will be kept and can be restored later.">
                    <input type="hidden" name="id" value="@Model.Chapter.ChapterID" />
                    <input type="hidden" name="bookId" value="@Model.Book.BookID" />
                    <button class="dropdown-item text-danger" type="submit">Archive Chapter</button>
                </form>
            </div>
        </div>
    </div>
</div>

@if (Model.PendingCharacterSuggestionCount > 0 || Model.PendingAssetSuggestionCount > 0 || Model.PendingLocationSuggestionCount > 0)
{
    <section class="list-section word-companion-suggestion-summary">
        <h2>Word Companion Suggestions</h2>
        @if (Model.PendingCharacterSuggestionCount > 0)
        {
            <p class="muted">@Model.PendingCharacterSuggestionCount character suggestion@(Model.PendingCharacterSuggestionCount == 1 ? "" : "s") awaiting review in this chapter.</p>
        }
        @if (Model.PendingAssetSuggestionCount > 0)
        {
            <p class="muted">@Model.PendingAssetSuggestionCount asset suggestion@(Model.PendingAssetSuggestionCount == 1 ? "" : "s") awaiting review in this chapter.</p>
        }
        @if (Model.PendingLocationSuggestionCount > 0)
        {
            <p class="muted">@Model.PendingLocationSuggestionCount location suggestion@(Model.PendingLocationSuggestionCount == 1 ? "" : "s") awaiting review in this chapter.</p>
        }
    </section>
}

<section class="list-section">
    <h2>Scenes <help-icon key="chapters.scenes" /></h2>
    @if (!Model.Scenes.Any())
    {
        <p class="muted">No scenes yet.</p>
    }
    else
    {
        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>Scene</th>
                        <th>Title</th>
                        <th>Status</th>
                        <th>Manuscript</th>
                        <th>Time</th>
                        <th>Summary</th>
                        <th>Order</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var scene in Model.Scenes)
                    {
                        <tr>
                            <td class="narrow">@scene.SceneNumber</td>
                            <td>
                                <a asp-controller="Scenes" asp-action="Edit" asp-route-id="@scene.SceneID">@scene.SceneTitle</a>
                                @if (scene.PendingCharacterSuggestionCount > 0 || scene.PendingAssetSuggestionCount > 0 || scene.PendingLocationSuggestionCount > 0)
                                {
                                    var suggestionParts = new List<string>();
                                    if (scene.PendingCharacterSuggestionCount > 0)
                                    {
                                        suggestionParts.Add($"{scene.PendingCharacterSuggestionCount} Character{(scene.PendingCharacterSuggestionCount == 1 ? "" : "s")}");
                                    }
                                    if (scene.PendingAssetSuggestionCount > 0)
                                    {
                                        suggestionParts.Add($"{scene.PendingAssetSuggestionCount} Asset{(scene.PendingAssetSuggestionCount == 1 ? "" : "s")}");
                                    }
                                    if (scene.PendingLocationSuggestionCount > 0)
                                    {
                                        suggestionParts.Add($"{scene.PendingLocationSuggestionCount} Location{(scene.PendingLocationSuggestionCount == 1 ? "" : "s")}");
                                    }
                                    <span class="status-pill word-companion-suggestion-pill">@string.Join(" • ", suggestionParts)</span>
                                }
                            </td>
                            <td><span class="status-pill">@scene.RevisionStatusName</span></td>
                            <td><span class="status-pill">@(scene.IsLinkedToManuscript ? "🔗 Linked" : "📝 Planned")</span></td>
                            <td>@scene.TimeLabel</td>
                            <td>@scene.Summary</td>
                            <td>
                                <div class="order-buttons">
                                    <form asp-controller="Scenes" asp-action="MoveUp" method="post">
                                        <input type="hidden" name="id" value="@scene.SceneID" />
                                        <input type="hidden" name="chapterId" value="@Model.Chapter.ChapterID" />
                                        <button class="btn btn-outline-secondary btn-sm" type="submit">Up</button>
                                    </form>
                                    <form asp-controller="Scenes" asp-action="MoveDown" method="post">
                                        <input type="hidden" name="id" value="@scene.SceneID" />
                                        <input type="hidden" name="chapterId" value="@Model.Chapter.ChapterID" />
                                        <button class="btn btn-outline-secondary btn-sm" type="submit">Down</button>
                                    </form>
                                </div>
                            </td>
                            <td class="text-end">
                                <a class="btn btn-outline-secondary btn-sm" asp-controller="Timeline" asp-action="Index" asp-route-projectId="@Model.Project.ProjectID" asp-route-bookId="@Model.Book.BookID" asp-route-selectedSceneId="@scene.SceneID">Open in timeline</a>
                                <a class="btn btn-outline-secondary btn-sm" asp-controller="Scenes" asp-action="Edit" asp-route-id="@scene.SceneID">Edit</a>
                                <form asp-controller="Scenes" asp-action="Archive" method="post" class="archive-button-form" data-confirm-message="Archive this scene?&#10;&#10;This will hide it from active lists and timelines, but the data will be kept and can be restored later.">
                                    <input type="hidden" name="id" value="@scene.SceneID" />
                                    <input type="hidden" name="chapterId" value="@Model.Chapter.ChapterID" />
                                    <button class="btn btn-outline-danger btn-sm" type="submit">Archive</button>
                                </form>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
    }
</section>
