@model ScenarioTimelineViewModel
@{
    ViewData["Title"] = $"{Model.Scenario.ScenarioName} Scenario";
    ViewData["ShellClass"] = "plotline-shell-wide";
    var scenesByChapter = Model.Scenes.GroupBy(x => x.ProposedChapterID).ToDictionary(x => x.Key, x => x.OrderBy(s => s.ProposedSortOrder).ToList());
    var orderedScenes = Model.Scenes.OrderBy(x => x.ProposedSortOrder).ToList();
}

<section class="scenario-banner">
    <div>
        <p class="eyebrow">Scenario Mode</p>
        <h1>@Model.Scenario.ScenarioName</h1>
        <p>@Model.Scenario.Description</p>
    </div>
    <div class="button-row">
        <a class="btn btn-outline-secondary" asp-controller="Timeline" asp-action="Index" asp-route-projectId="@Model.Project.ProjectID" asp-route-bookId="@Model.Scenario.BookID">Return to Main Timeline</a>
        <form asp-action="Validate" method="post">
            <input type="hidden" name="id" value="@Model.Scenario.ScenarioID" />
            <button class="btn btn-outline-primary" type="submit">Validate Scenario</button>
        </form>
        <a class="btn btn-outline-primary" asp-action="Compare" asp-route-id="@Model.Scenario.ScenarioID">Compare</a>
        <form asp-action="Archive" method="post" data-confirm-message="Archive this scenario?&#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.Scenario.ScenarioID" />
            <input type="hidden" name="projectId" value="@Model.Project.ProjectID" />
            <button class="btn btn-outline-danger" type="submit">Archive Scenario</button>
        </form>
    </div>
</section>

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

<section class="scenario-warning-strip">
    <strong>@Model.Warnings.Count scenario warning@(Model.Warnings.Count == 1 ? "" : "s")</strong>
    <span>These warnings belong to the sandbox and do not affect the main warning board.</span>
</section>

<section class="scenario-board">
    <div class="scenario-scroll">
        @foreach (var chapter in Model.Chapters)
        {
            var chapterScenes = scenesByChapter.GetValueOrDefault(chapter.ChapterID, []);
            if (!chapterScenes.Any())
            {
                continue;
            }

            <article class="scenario-chapter">
                <header>
                    <span>Chapter @chapter.ChapterNumber:g</span>
                    <strong>@chapter.ChapterTitle</strong>
                </header>
                <div class="scenario-scene-row">
                    @foreach (var scene in chapterScenes)
                    {
                        var previous = orderedScenes.TakeWhile(x => x.SceneID != scene.SceneID).LastOrDefault();
                        var next = orderedScenes.SkipWhile(x => x.SceneID != scene.SceneID).Skip(1).FirstOrDefault();
                        <article class="scenario-scene-card">
                            <div class="scenario-scene-title">
                                <span>Scene @scene.SceneNumber:g</span>
                                <strong>@scene.SceneTitle</strong>
                            </div>
                            <div class="story-bible-meta">
                                <span>@scene.RevisionStatusName</span>
                                <span>@scene.TimeLabel</span>
                                @if (scene.WarningCount > 0)
                                {
                                    <span class="text-danger">@scene.WarningCount warning@(scene.WarningCount == 1 ? "" : "s")</span>
                                }
                            </div>
                            <p>@scene.Summary</p>
                            <div class="scenario-mini-data">
                                @if (!string.IsNullOrWhiteSpace(scene.ThreadLabels)) { <span>Threads: @scene.ThreadLabels</span> }
                                @if (!string.IsNullOrWhiteSpace(scene.AssetLabels)) { <span>Assets: @scene.AssetLabels</span> }
                                @if (!string.IsNullOrWhiteSpace(scene.CharacterLabels)) { <span>Characters: @scene.CharacterLabels</span> }
                                @if (!string.IsNullOrWhiteSpace(scene.LocationLabel)) { <span>Location: @scene.LocationLabel</span> }
                            </div>
                            <div class="scenario-move-actions">
                                @if (previous is not null)
                                {
                                    <form asp-action="MoveScene" method="post">
                                        <input type="hidden" name="scenarioId" value="@Model.Scenario.ScenarioID" />
                                        <input type="hidden" name="sceneId" value="@scene.SceneID" />
                                        <input type="hidden" name="anchorSceneId" value="@previous.SceneID" />
                                        <input type="hidden" name="position" value="Before" />
                                        <button class="btn btn-outline-secondary btn-sm" type="submit">Move up</button>
                                    </form>
                                }
                                @if (next is not null)
                                {
                                    <form asp-action="MoveScene" method="post">
                                        <input type="hidden" name="scenarioId" value="@Model.Scenario.ScenarioID" />
                                        <input type="hidden" name="sceneId" value="@scene.SceneID" />
                                        <input type="hidden" name="anchorSceneId" value="@next.SceneID" />
                                        <input type="hidden" name="position" value="After" />
                                        <button class="btn btn-outline-secondary btn-sm" type="submit">Move down</button>
                                    </form>
                                }
                            </div>
                            <form asp-action="MoveScene" method="post" class="scenario-chapter-form">
                                <input type="hidden" name="scenarioId" value="@Model.Scenario.ScenarioID" />
                                <input type="hidden" name="sceneId" value="@scene.SceneID" />
                                <input type="hidden" name="position" value="End" />
                                <select name="targetChapterId" class="form-select form-select-sm">
                                    @foreach (var chapterOption in Model.Chapters)
                                    {
                                        <option value="@chapterOption.ChapterID" selected="@(chapterOption.ChapterID == scene.ProposedChapterID)">Ch @chapterOption.ChapterNumber:g</option>
                                    }
                                </select>
                                <button class="btn btn-outline-primary btn-sm" type="submit">Move to chapter</button>
                            </form>
                            <a asp-controller="Scenes" asp-action="Edit" asp-route-id="@scene.SceneID">Open scene</a>
                        </article>
                    }
                </div>
            </article>
        }
    </div>
</section>

<section class="story-bible-section">
    <div class="story-bible-section-heading">
        <div>
            <p class="eyebrow">Scenario warnings</p>
            <h2>Validation Notes</h2>
        </div>
        <span class="soft-count">@Model.Warnings.Count</span>
    </div>
    @if (!Model.Warnings.Any())
    {
        <p class="muted">Run scenario validation to check this order.</p>
    }
    else
    {
        <div class="attention-list">
            @foreach (var warning in Model.Warnings)
            {
                <article>
                    <span class="badge text-bg-light">Scenario warning</span>
                    <strong>@warning.WarningTypeName</strong>
                    <p>@warning.Message</p>
                    @if (!string.IsNullOrWhiteSpace(warning.Details))
                    {
                        <p class="muted">@warning.Details</p>
                    }
                </article>
            }
        </div>
    }
</section>
