@model StoryIntelligenceExistingChapterQueueViewModel
@{
    ViewData["Title"] = "Queue Story Intelligence From Chapter";
}

<div class="page-heading compact">
    <div>
        <p class="eyebrow">Admin utility</p>
        <h1>Queue from existing chapter</h1>
        <p class="lead-text">Run the persisted Story Intelligence pipeline from read-only PlotDirector chapter data.</p>
    </div>
</div>

<nav class="mb-3" aria-label="Breadcrumb">
    <a asp-controller="Projects" asp-action="Index">Projects</a>
    <span class="muted"> / </span>
    <a asp-action="Index">Admin</a>
    <span class="muted"> / Queue from existing chapter</span>
</nav>

@if (!string.IsNullOrWhiteSpace(Model.ErrorMessage))
{
    <div class="alert alert-danger">@Model.ErrorMessage</div>
}

@if (!string.IsNullOrWhiteSpace(Model.WarningMessage))
{
    <div class="alert alert-warning">@Model.WarningMessage</div>
}

<section class="edit-panel">
    <form method="get" asp-action="StoryIntelligenceExistingChapter">
        <div class="row g-3">
            <div class="col-md-4">
                <label class="form-label" for="ProjectID">Project</label>
                <select class="form-select" id="ProjectID" name="ProjectID">
                    <option value="">Choose project</option>
                    @foreach (var project in Model.Projects)
                    {
                        <option value="@project.Value" selected="@(Model.Form.ProjectID == project.Value)">@project.Text</option>
                    }
                </select>
            </div>
            <div class="col-md-4">
                <label class="form-label" for="BookID">Book</label>
                <select class="form-select" id="BookID" name="BookID">
                    <option value="">Choose book</option>
                    @foreach (var book in Model.Books)
                    {
                        <option value="@book.Value" selected="@(Model.Form.BookID == book.Value)">@book.Text</option>
                    }
                </select>
            </div>
            <div class="col-md-4">
                <label class="form-label" for="ChapterID">Chapter</label>
                <select class="form-select" id="ChapterID" name="ChapterID">
                    <option value="">Choose chapter</option>
                    @foreach (var chapter in Model.Chapters)
                    {
                        <option value="@chapter.Value" selected="@(Model.Form.ChapterID == chapter.Value)">@chapter.Text</option>
                    }
                </select>
            </div>
        </div>
        <div class="button-row mt-3">
            <button type="submit" class="btn btn-outline-primary">Load selection</button>
            <a class="btn btn-outline-secondary" asp-action="StoryIntelligenceRuns">Saved runs</a>
        </div>
    </form>
</section>

@if (Model.SelectedChapter is not null)
{
    <section class="edit-panel">
        <h2>Selected source</h2>
        <dl class="row mb-0">
            <dt class="col-sm-3">Project</dt>
            <dd class="col-sm-9">@Model.SelectedChapter.ProjectName</dd>
            <dt class="col-sm-3">Book</dt>
            <dd class="col-sm-9">@DisplayBook(Model.SelectedChapter)</dd>
            <dt class="col-sm-3">Chapter</dt>
            <dd class="col-sm-9">Chapter @Model.SelectedChapter.ChapterNumber: @Model.SelectedChapter.ChapterTitle</dd>
            <dt class="col-sm-3">Word count</dt>
            <dd class="col-sm-9">@DisplayCount(Model.SelectedChapter.SourceWordCount)</dd>
            <dt class="col-sm-3">Character count</dt>
            <dd class="col-sm-9">@DisplayCount(Model.SelectedChapter.SourceCharacterCount)</dd>
            <dt class="col-sm-3">Text source</dt>
            <dd class="col-sm-9">@(Model.SelectedChapter.HasSourceText ? "Stored chapter manuscript text is available." : "No stored chapter manuscript text is available.")</dd>
        </dl>

        <form method="post" asp-action="QueueStoryIntelligenceExistingChapter" class="mt-3">
            @Html.AntiForgeryToken()
            <input type="hidden" name="ProjectID" value="@Model.Form.ProjectID" />
            <input type="hidden" name="BookID" value="@Model.Form.BookID" />
            <input type="hidden" name="ChapterID" value="@Model.Form.ChapterID" />
            <button type="submit" class="btn btn-primary" disabled="@(!Model.SelectedChapter.HasSourceText)">Queue Story Intelligence run</button>
        </form>
    </section>
}

@functions {
    private static string DisplayCount(int? value)
        => value.HasValue ? value.Value.ToString("N0") : "Not available";

    private static string DisplayBook(StoryIntelligenceExistingChapterSource source)
        => string.IsNullOrWhiteSpace(source.BookSubtitle)
            ? source.BookTitle
            : $"{source.BookTitle}: {source.BookSubtitle}";
}
