@model StoryIntelligenceFullChapterTestViewModel
@{
    ViewData["Title"] = "Full Chapter Story Intelligence Test";
    var canQueue = Model.Preflight is not null && string.IsNullOrWhiteSpace(Model.ErrorMessage);
}

<div class="page-heading compact">
    <div>
        <p class="eyebrow">Admin utility</p>
        <h1>Full chapter test</h1>
        <p class="lead-text">Queue one large chapter into the persisted Story Intelligence runner without changing PlotDirector story 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"> / Full chapter test</span>
</nav>

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

<section class="edit-panel">
    <form asp-action="StoryIntelligenceFullChapterTest" method="post" enctype="multipart/form-data">
        @Html.AntiForgeryToken()

        <div class="row g-3">
            <div class="col-md-6">
                <label class="form-label" asp-for="Form.SourceLabel">Source label</label>
                <input class="form-control" asp-for="Form.SourceLabel" placeholder="Optional label for this test run" />
            </div>
            <div class="col-md-2">
                <label class="form-label" asp-for="Form.ProjectID">Project ID</label>
                <input class="form-control" asp-for="Form.ProjectID" />
            </div>
            <div class="col-md-2">
                <label class="form-label" asp-for="Form.BookID">Book ID</label>
                <input class="form-control" asp-for="Form.BookID" />
            </div>
            <div class="col-md-2">
                <label class="form-label" asp-for="Form.ChapterID">Chapter ID</label>
                <input class="form-control" asp-for="Form.ChapterID" />
            </div>
            <div class="col-md-6">
                <label class="form-label" asp-for="Form.ChapterStructureModel">Chapter Structure model</label>
                <select class="form-select" asp-for="Form.ChapterStructureModel">
                    @foreach (var modelOption in Model.ModelOptions)
                    {
                        <option value="@modelOption">@modelOption</option>
                    }
                </select>
            </div>
            <div class="col-md-6">
                <label class="form-label" asp-for="Form.SceneIntelligenceModel">Scene Intelligence model</label>
                <select class="form-select" asp-for="Form.SceneIntelligenceModel">
                    @foreach (var modelOption in Model.ModelOptions)
                    {
                        <option value="@modelOption">@modelOption</option>
                    }
                </select>
            </div>
        </div>

        <div class="mt-3">
            <label class="form-label" for="textFile">Upload .txt file</label>
            <input class="form-control" id="textFile" name="textFile" type="file" accept=".txt,text/plain" />
            @if (!string.IsNullOrWhiteSpace(Model.SourceFileName))
            {
                <p class="form-text mb-0">Loaded @Model.SourceFileName (@DisplayBytes(Model.SourceFileSizeBytes)).</p>
            }
        </div>

        <div class="mt-3">
            <label class="form-label" asp-for="Form.ChapterText">Chapter text</label>
            <textarea class="form-control font-monospace" asp-for="Form.ChapterText" rows="24" maxlength="500000"></textarea>
        </div>

        <div class="button-row mt-3">
            <button class="btn btn-outline-primary" type="submit" name="submitAction" value="Analyze">Analyze pre-flight</button>
            <button class="btn btn-primary" type="submit" name="submitAction" value="Queue" disabled="@(!canQueue)">Queue persisted run</button>
            <a class="btn btn-outline-secondary" asp-action="StoryIntelligenceRuns">Open monitor</a>
        </div>
    </form>
</section>

@if (Model.Preflight is not null)
{
    <section class="edit-panel">
        <h2>Pre-flight</h2>
        <div class="row g-3">
            <div class="col-sm-6 col-lg-3">
                <div class="border rounded p-3 h-100">
                    <p class="eyebrow">Characters</p>
                    <h3 class="mb-0">@Model.Preflight.CharacterCount.ToString("N0")</h3>
                </div>
            </div>
            <div class="col-sm-6 col-lg-3">
                <div class="border rounded p-3 h-100">
                    <p class="eyebrow">Words</p>
                    <h3 class="mb-0">@Model.Preflight.WordCount.ToString("N0")</h3>
                </div>
            </div>
            <div class="col-sm-6 col-lg-3">
                <div class="border rounded p-3 h-100">
                    <p class="eyebrow">Paragraphs</p>
                    <h3 class="mb-0">@Model.Preflight.ParagraphCount.ToString("N0")</h3>
                </div>
            </div>
            <div class="col-sm-6 col-lg-3">
                <div class="border rounded p-3 h-100">
                    <p class="eyebrow">Input tokens</p>
                    <h3 class="mb-0">@Model.Preflight.EstimatedInputTokens.ToString("N0")</h3>
                </div>
            </div>
        </div>

        <dl class="mt-3 mb-0">
            <dt>Estimated pipeline input tokens</dt>
            <dd>@Model.Preflight.EstimatedPipelineInputTokens.ToString("N0")</dd>
            <dt>Selected models</dt>
            <dd>Chapter Structure: @Model.Form.ChapterStructureModel; Scene Intelligence: @Model.Form.SceneIntelligenceModel</dd>
            <dt>Estimated input cost</dt>
            <dd>@DisplayCost(Model.Preflight.EstimatedInputCostGBP, "GBP") / @DisplayCost(Model.Preflight.EstimatedInputCostUSD, "USD")</dd>
        </dl>

        @if (Model.Preflight.Warnings.Count > 0)
        {
            <div class="alert alert-warning mt-3 mb-0">
                <ul class="mb-0">
                    @foreach (var warning in Model.Preflight.Warnings)
                    {
                        <li>@warning</li>
                    }
                </ul>
            </div>
        }
        else
        {
            <div class="alert alert-success mt-3 mb-0">No pre-flight warnings detected.</div>
        }
    </section>
}

@functions {
    private static string DisplayBytes(long? bytes)
        => bytes.HasValue ? $"{bytes.Value:N0} bytes" : "None";

    private static string DisplayCost(decimal? value, string currency)
        => value.HasValue ? $"{value.Value:0.000000} {currency}" : "Not configured";
}
