@model StoryIntelligencePipelineHeaderViewModel
@{
    var reviewStages = new[]
    {
        new PipelineStage("Review Scenes", "Scenes", true),
        new PipelineStage("Review Characters", "Characters", true),
        new PipelineStage("Review Locations", "Locations", true),
        new PipelineStage("Review Assets", "Assets", true),
        new PipelineStage("Review Relationships", "Relationships", true),
        new PipelineStage("Review Knowledge", "Knowledge", true)
    };
    var implementedReviewStages = reviewStages.Where(stage => stage.IsImplemented).ToList();
    var currentReviewIndex = implementedReviewStages.FindIndex(stage => string.Equals(stage.StageName, Model.CurrentStage, StringComparison.OrdinalIgnoreCase));
    var isReviewPhase = currentReviewIndex >= 0;
    var isComplete = string.Equals(Model.CurrentStage, "Complete", StringComparison.OrdinalIgnoreCase);
    var isAnalyse = string.Equals(Model.CurrentStage, "Analyse Manuscript", StringComparison.OrdinalIgnoreCase);
    var activePhase = isComplete ? 4 : isReviewPhase ? 3 : isAnalyse ? 2 : 1;
    var progressPercent = isReviewPhase
        ? Math.Clamp(((currentReviewIndex + 1) * 100) / implementedReviewStages.Count, 0, 100)
        : isComplete ? 100 : 100;
    var currentStageNumber = isReviewPhase ? currentReviewIndex + 1 : isComplete ? implementedReviewStages.Count : 0;
}

<section class="story-pipeline-header" aria-label="Story Intelligence pipeline">
    <div class="story-pipeline-current">
        <p class="eyebrow">Phase @activePhase</p>
        <h2>@CurrentPhaseTitle(activePhase)</h2>
        @if (isReviewPhase)
        {
            <p>Stage @currentStageNumber of @implementedReviewStages.Count</p>
        }
        else if (isComplete)
        {
            <p>Final import and completion</p>
        }
        else if (isAnalyse)
        {
            <p>Story Intelligence Analysis</p>
        }
        else
        {
            <p>Prepare the manuscript for analysis</p>
        }
    </div>

    <div class="onboarding-progress story-pipeline-progress" aria-label="@CurrentPhaseTitle(activePhase) progress">
        <span>@ProgressLabel(activePhase, Model.CurrentStage)</span>
        <div class="onboarding-progress-track">
            <span style="width:@progressPercent%"></span>
        </div>
    </div>

    <ol class="story-pipeline-phase-list">
        <li class="@(activePhase > 1 ? "is-complete" : activePhase == 1 ? "is-current" : "is-upcoming")">
            <strong>Prepare Manuscript</strong>
            <span>7 completed steps</span>
            <ul>
                <li>Welcome</li>
                <li>Writing Preferences</li>
                <li>Project</li>
                <li>Book</li>
                <li>Connect Word</li>
                <li>Scan Manuscript</li>
                <li>Review Chapters</li>
            </ul>
        </li>
        <li class="@(activePhase > 2 ? "is-complete" : activePhase == 2 ? "is-current" : "is-upcoming")">
            <strong>Analyse Manuscript</strong>
            <span>Story Intelligence Analysis</span>
        </li>
        <li class="@(activePhase > 3 ? "is-complete" : activePhase == 3 ? "is-current" : "is-upcoming")">
            <strong>Review Story Intelligence</strong>
            <span>@ReviewSummary(isReviewPhase, currentStageNumber, implementedReviewStages.Count)</span>
            <ul class="story-pipeline-review-list">
                @foreach (var stage in reviewStages)
                {
                    var index = implementedReviewStages.FindIndex(item => string.Equals(item.StageName, stage.StageName, StringComparison.OrdinalIgnoreCase));
                    var stageClass = !stage.IsImplemented
                        ? "is-upcoming"
                        : isComplete || index < currentReviewIndex
                            ? "is-complete"
                            : index == currentReviewIndex
                                ? "is-current"
                                : "is-upcoming";
                    <li class="@stageClass">
                        <span aria-hidden="true">@StageMarker(stageClass, stage.IsImplemented)</span>
                        <strong>@stage.Label</strong>
                        @if (!stage.IsImplemented)
                        {
                            <em>Coming Soon</em>
                        }
                    </li>
                }
            </ul>
        </li>
        <li class="@(activePhase == 4 ? "is-current" : "is-upcoming")">
            <strong>Build Story Database</strong>
            <span>Final import and completion</span>
        </li>
    </ol>
</section>

@functions {
    private sealed record PipelineStage(string StageName, string Label, bool IsImplemented);

    private static string CurrentPhaseTitle(int phase)
        => phase switch
        {
            1 => "Prepare Manuscript",
            2 => "Analyse Manuscript",
            3 => "Review Story Intelligence",
            _ => "Build Story Database"
        };

    private static string ProgressLabel(int phase, string currentStage)
        => phase switch
        {
            3 => currentStage,
            4 => "Finalising import",
            _ => "Completed"
        };

    private static string ReviewSummary(bool isReviewPhase, int currentStageNumber, int implementedStageCount)
        => isReviewPhase
            ? $"{Math.Max(0, currentStageNumber - 1)} of {implementedStageCount} review stages completed"
            : $"{implementedStageCount} review stages completed";

    private static string StageMarker(string stageClass, bool isImplemented)
        => !isImplemented ? "○" : stageClass == "is-current" ? "►" : stageClass == "is-complete" ? "✓" : "○";
}
