@model SceneContinuityEditorViewModel
@{
    var scene = Model.Scene;
    var dashboard = Model.Dashboard;
    var persistedWarnings = dashboard.ProjectWarnings
        .Where(x => x.Source == "Persisted" && x.ContinuityWarningID.HasValue)
        .ToList();
    var generatedWarnings = dashboard.ProjectWarnings
        .Where(x => x.Source != "Persisted")
        .ToList();
    var ageWarnings = scene.AgeContinuityWarnings;
    var totalWarnings = persistedWarnings.Count + generatedWarnings.Count + ageWarnings.Count;
}

<div class="scene-inspector-editor-form">
    <div class="scene-inspector-editor-fields">
        <section class="scene-structure-editor-section">
            <div class="scene-structure-editor-heading">
                <span>Continuity warnings</span>
                <strong>@totalWarnings</strong>
            </div>

            <form asp-controller="Warnings" asp-action="ValidateScene" method="post" class="scene-structure-editor-card scene-structure-editor-form" data-scene-editor-form>
                @Html.AntiForgeryToken()
                <input type="hidden" name="projectId" value="@scene.ReturnProjectID" />
                <input type="hidden" name="bookId" value="@scene.ReturnBookID" />
                <input type="hidden" name="sceneId" value="@scene.SceneID" />
                <input type="hidden" name="returnToTimeline" value="@scene.ReturnToTimeline.ToString().ToLowerInvariant()" />

                <div class="scene-inspector-editor-error" data-scene-editor-error hidden></div>

                <p class="muted">Run the existing scene validation checks and refresh this review list.</p>
                <div class="scene-structure-action-row">
                    <button class="btn btn-sm btn-primary" type="submit" data-scene-editor-save>Revalidate scene</button>
                </div>
            </form>
        </section>

        <section class="scene-structure-editor-section">
            <div class="scene-structure-editor-heading">
                <span>Review warnings</span>
                <strong>@totalWarnings</strong>
            </div>

            @if (totalWarnings == 0)
            {
                <p class="muted">No continuity warnings are recorded for this scene.</p>
            }
            else
            {
                <div class="scene-structure-editor-list">
                    @foreach (var warning in persistedWarnings)
                    {
                        <article class="scene-structure-editor-card">
                            <div class="scene-structure-card-header">
                                <div>
                                    <h4>@warning.WarningTypeDisplayName</h4>
                                    <p>@warning.Category</p>
                                </div>
                                <span class="scene-structure-marker">@warning.Severity</span>
                            </div>

                            <p>@warning.Message</p>
                            @if (!string.IsNullOrWhiteSpace(warning.Details))
                            {
                                <p class="muted">@warning.Details</p>
                            }

                            <div class="scene-inspector-v2-chip-row">
                                @if (warning.IsDismissed)
                                {
                                    <span class="status-pill">Dismissed</span>
                                }
                                else if (warning.IsIntentional)
                                {
                                    <span class="status-pill">Intentional</span>
                                }
                                else
                                {
                                    <span class="status-pill">Active</span>
                                }
                                @if (!string.IsNullOrWhiteSpace(warning.EntityType))
                                {
                                    <span class="status-pill">@warning.EntityType</span>
                                }
                            </div>

                            @if (warning.CanInvestigate)
                            {
                                <div class="scene-structure-action-row">
                                    <a class="btn btn-sm btn-outline-primary"
                                       asp-controller="ContinuityExplorer"
                                       asp-action="Index"
                                       asp-route-projectId="@dashboard.Project.ProjectID"
                                       asp-route-mode="@warning.InvestigationDisplayMode"
                                       asp-route-characterId="@warning.CharacterID"
                                       asp-route-assetId="@warning.AssetID"
                                       asp-route-sceneId="@warning.InvestigationSceneId">Investigate</a>
                                </div>
                            }

                            @if (warning.IsDismissed || warning.IsIntentional)
                            {
                                <form asp-controller="Warnings" asp-action="Restore" method="post" class="scene-structure-delete-form" data-scene-editor-form>
                                    @Html.AntiForgeryToken()
                                    <input type="hidden" name="warningId" value="@warning.ContinuityWarningID" />
                                    <input type="hidden" name="projectId" value="@dashboard.Project.ProjectID" />
                                    <input type="hidden" name="bookId" value="@dashboard.BookID" />
                                    <input type="hidden" name="sceneId" value="@scene.SceneID" />
                                    <div class="scene-inspector-editor-error" data-scene-editor-error hidden></div>
                                    <button class="btn btn-sm btn-outline-secondary" type="submit" data-scene-editor-save>Restore warning</button>
                                </form>
                            }
                            else
                            {
                                <div class="scene-structure-action-row">
                                    <form asp-controller="Warnings" asp-action="Dismiss" method="post" class="scene-structure-delete-form" data-scene-editor-form>
                                        @Html.AntiForgeryToken()
                                        <input type="hidden" name="warningId" value="@warning.ContinuityWarningID" />
                                        <input type="hidden" name="projectId" value="@dashboard.Project.ProjectID" />
                                        <input type="hidden" name="bookId" value="@dashboard.BookID" />
                                        <input type="hidden" name="sceneId" value="@scene.SceneID" />
                                        <input type="hidden" name="returnToTimeline" value="@scene.ReturnToTimeline.ToString().ToLowerInvariant()" />
                                        <div class="scene-inspector-editor-error" data-scene-editor-error hidden></div>
                                        <button class="btn btn-sm btn-outline-secondary" type="submit" data-scene-editor-save>Dismiss</button>
                                    </form>

                                    <form asp-controller="Warnings" asp-action="MarkIntentional" method="post" class="scene-structure-delete-form" data-scene-editor-form>
                                        @Html.AntiForgeryToken()
                                        <input type="hidden" name="warningId" value="@warning.ContinuityWarningID" />
                                        <input type="hidden" name="projectId" value="@dashboard.Project.ProjectID" />
                                        <input type="hidden" name="bookId" value="@dashboard.BookID" />
                                        <input type="hidden" name="sceneId" value="@scene.SceneID" />
                                        <input type="hidden" name="returnToTimeline" value="@scene.ReturnToTimeline.ToString().ToLowerInvariant()" />
                                        <div class="scene-inspector-editor-error" data-scene-editor-error hidden></div>
                                        <button class="btn btn-sm btn-outline-secondary" type="submit" data-scene-editor-save>Mark intentional</button>
                                    </form>
                                </div>
                            }
                        </article>
                    }

                    @foreach (var warning in generatedWarnings)
                    {
                        <article class="scene-structure-editor-card">
                            <div class="scene-structure-card-header">
                                <div>
                                    <h4>@warning.WarningTypeDisplayName</h4>
                                    <p>@warning.Category</p>
                                </div>
                                <span class="scene-structure-marker">@warning.Severity</span>
                            </div>
                            <p>@warning.Message</p>
                            <div class="scene-inspector-v2-chip-row">
                                <span class="status-pill">Generated</span>
                                <span class="status-pill">Review only</span>
                            </div>
                        </article>
                    }

                    @foreach (var warning in ageWarnings)
                    {
                        <article class="scene-structure-editor-card">
                            <div class="scene-structure-card-header">
                                <div>
                                    <h4>@warning.WarningTypeName</h4>
                                    <p>Character age continuity</p>
                                </div>
                                <span class="scene-structure-marker">@warning.SeverityName</span>
                            </div>
                            <p>@warning.Message</p>
                            @if (!string.IsNullOrWhiteSpace(warning.Details))
                            {
                                <p class="muted">@warning.Details</p>
                            }
                            <div class="scene-inspector-v2-chip-row">
                                <span class="status-pill">Generated</span>
                                <span class="status-pill">Review only</span>
                            </div>
                        </article>
                    }
                </div>
            }
        </section>
    </div>

    <div class="scene-inspector-editor-footer">
        <button type="button" class="btn btn-sm btn-outline-secondary" data-scene-editor-close>Close</button>
    </div>
</div>
