@model StoryIntelligenceOverviewViewModel
@{
    ViewData["Title"] = "Analyse manuscript";
}

<section class="onboarding-shell" aria-labelledby="story-intelligence-title">
    <div class="onboarding-panel">
        <partial name="_StoryIntelligencePipelineHeader" model="@(new StoryIntelligencePipelineHeaderViewModel { CurrentStage = "Analyse Manuscript" })" />
        <div class="onboarding-copy">
            <p class="eyebrow">Analyse manuscript</p>
            <h1 id="story-intelligence-title">Analyse Manuscript</h1>
            <p>PlotDirector will analyse approved chapters in the background and prepare scene suggestions for review.</p>
        </div>

        @if (TempData["OnboardingStoryIntelligenceError"] is string error)
        {
            <div class="alert alert-danger">@error</div>
        }
        @if (TempData["OnboardingStoryIntelligenceMessage"] is string message)
        {
            <div class="alert alert-success">@message</div>
        }

        @if (Model.ExistingBatchID.HasValue)
        {
            <section class="onboarding-story-card">
                <h2>Analysis has already started</h2>
                <p>You can continue reviewing progress for the approved chapters.</p>
                <div class="onboarding-actions">
                    <a class="btn btn-primary" asp-action="StoryIntelligenceReview" asp-route-batchId="@Model.ExistingBatchID">Continue pipeline</a>
                    <form asp-action="ScanManuscript" method="post">
                        <button class="btn btn-outline-secondary" type="submit">Scan manuscript again</button>
                    </form>
                </div>
            </section>
        }
        else
        {
            <div class="onboarding-story-facts">
                <div>
                    <span>Chapters</span>
                    <strong>@Model.ApprovedChapterCount</strong>
                    <p>Approved chapters from your manuscript review.</p>
                </div>
                <div>
                    <span>Words</span>
                    <strong>@Model.ApprovedWordCount.ToString("N0")</strong>
                    <p>Chapter text ready for analysis.</p>
                </div>
                <div>
                    <span>Estimated time</span>
                    <strong>@EstimateDuration(Model.ApprovedWordCount)</strong>
                    <p>The analysis runs in the background, so you can leave this page and return later.</p>
                </div>
                <div>
                    <span>Review</span>
                    <strong>You approve creation</strong>
                    <p>Scenes are created only after the analysis completes and you choose Create scenes.</p>
                </div>
            </div>

            <section class="onboarding-form-section">
                <p>Nothing will be created yet. PlotDirector will prepare scene suggestions for review.</p>
                <p>Scenes are reviewed first. Character review follows after scenes are created, with locations, assets, relationships and knowledge shown as upcoming Story Intelligence stages.</p>
            </section>

            @if (!string.IsNullOrWhiteSpace(Model.Message))
            {
                <section class="onboarding-form-section">
                    <p>@Model.Message</p>
                    @if (Model.MissingChapterTextCount > 0)
                    {
                        <p class="text-danger">@Model.MissingChapterTextCount chapter(s) do not include scanned text. Scan the manuscript again before analysing.</p>
                    }
                </section>
            }

            <div class="onboarding-actions">
                <a class="btn btn-outline-secondary" asp-action="ScanReview" asp-route-previewId="@Model.PreviewID">Back to chapter review</a>
                <form asp-action="SkipStoryIntelligence" method="post">
                    <button class="btn btn-outline-secondary" type="submit">Skip for now</button>
                </form>
                <form asp-action="StartStoryIntelligence" method="post">
                    <button class="btn btn-primary" type="submit" disabled="@(!Model.CanStart)">Analyse manuscript</button>
                </form>
            </div>
        }
    </div>
</section>

@functions {
    private static string EstimateDuration(int wordCount)
    {
        if (wordCount <= 0)
        {
            return "A few minutes";
        }

        var minutes = Math.Clamp((int)Math.Ceiling(wordCount / 3000m) * 2, 2, 30);
        return minutes <= 2 ? "About 2 minutes" : $"About {minutes:N0} minutes";
    }
}
