@model WarningDashboardViewModel
@{
    ViewData["Title"] = "Continuity Warnings";
}

<section class="context-header">
    <div class="context-title">
        <span class="eyebrow">Workspace context</span>
        <strong>@Model.Project.ProjectName</strong>
    </div>
    <nav class="context-nav" aria-label="Workspace navigation">
        <a asp-controller="Projects" asp-action="Details" asp-route-id="@Model.Project.ProjectID">Project</a>
        <a asp-controller="Timeline" asp-action="Index" asp-route-projectId="@Model.Project.ProjectID" asp-route-bookId="@Model.BookID">Timeline</a>
        <a asp-controller="Analytics" asp-action="Index" asp-route-projectId="@Model.Project.ProjectID" asp-route-bookId="@Model.BookID">Story health</a>
        <a asp-controller="Characters" asp-action="Index" asp-route-projectId="@Model.Project.ProjectID">Characters</a>
        <a asp-controller="StoryAssets" asp-action="Index" asp-route-projectId="@Model.Project.ProjectID">Assets</a>
        <a asp-controller="Locations" asp-action="Index" asp-route-projectId="@Model.Project.ProjectID">Locations</a>
        <a asp-controller="PlotThreads" asp-action="Index" asp-route-projectId="@Model.Project.ProjectID">Threads</a>
    </nav>
</section>

<div class="page-heading compact">
    <div>
        <p class="eyebrow">Continuity</p>
        <h1>@Model.Project.ProjectName <help-icon key="warnings.overview" /></h1>
    </div>
    <div class="button-row">
        <form asp-action="ValidateProject" method="post">
            <input type="hidden" name="projectId" value="@Model.Project.ProjectID" />
            <input type="hidden" name="bookId" value="@Model.BookID" />
            <button class="btn btn-primary" type="submit">Validate project</button>
        </form>
        @if (Model.BookID.HasValue)
        {
            <form asp-action="ValidateBook" method="post">
                <input type="hidden" name="projectId" value="@Model.Project.ProjectID" />
                <input type="hidden" name="bookId" value="@Model.BookID" />
                <button class="btn btn-outline-primary" type="submit">Validate book</button>
            </form>
        }
    </div>
</div>

@if (Model.LastValidationSummary is not null)
{
    <section class="validation-summary">
        <strong>Validation complete.</strong>
        <span>@Model.LastValidationSummary.ErrorCount errors</span>
        <span>@Model.LastValidationSummary.WarningCount warnings</span>
        <span>@Model.LastValidationSummary.InfoCount info</span>
    </section>
}

<section class="warning-filter-panel">
    <form method="get" class="row g-2 align-items-end">
        <input type="hidden" name="projectId" value="@Model.Project.ProjectID" />
        <div class="col-md-3">
            <label class="form-label">Book</label>
            <select name="bookId" asp-items="Model.BookOptions" class="form-select form-select-sm">
                <option value="">All books</option>
            </select>
        </div>
        <div class="col-md-3">
            <label class="form-label">Type</label>
            <select name="warningTypeId" asp-items="Model.WarningTypeOptions" class="form-select form-select-sm">
                <option value="">All types</option>
            </select>
        </div>
        <div class="col-md-2">
            <label class="form-label">Severity</label>
            <select name="warningSeverityId" asp-items="Model.SeverityOptions" class="form-select form-select-sm">
                <option value="">All severities</option>
            </select>
        </div>
        <div class="col-md-2">
            <label class="form-label">Entity</label>
            <select name="entityType" asp-items="Model.EntityTypeOptions" class="form-select form-select-sm">
                <option value="">All entities</option>
            </select>
        </div>
        <div class="col-md-2">
            <label class="form-check">
                <input class="form-check-input" name="includeDismissed" type="checkbox" value="true" checked="@Model.IncludeDismissed" />
                <span class="form-check-label">Dismissed</span>
            </label>
            <label class="form-check">
                <input class="form-check-input" name="includeIntentional" type="checkbox" value="true" checked="@Model.IncludeIntentional" />
                <span class="form-check-label">Intentional</span>
            </label>
        </div>
        <div class="col-12">
            <button class="btn btn-outline-primary btn-sm" type="submit">Filter warnings</button>
        </div>
    </form>
</section>

<section class="warning-dashboard-grid">
    <article class="plain-card">
        <h2>By severity</h2>
        @foreach (var count in Model.CountsBySeverity)
        {
            <div class="warning-count-row">
                <span class="severity-pill @count.Key.ToLowerInvariant()">@count.Key</span>
                <strong>@count.Value</strong>
            </div>
        }
    </article>
    <article class="plain-card">
        <h2>By type</h2>
        @foreach (var count in Model.CountsByType.Take(8))
        {
            <div class="warning-count-row">
                <span>@count.Key</span>
                <strong>@count.Value</strong>
            </div>
        }
    </article>
</section>

<section class="list-section">
    <h2>Warnings</h2>
    @if (!Model.Warnings.Any())
    {
        <p class="muted">No warnings match this view.</p>
    }
    else
    {
        <div class="warning-list">
            @foreach (var warning in Model.Warnings)
            {
                <article class="warning-card">
                    <div>
                        <div class="warning-card-title">
                            <span class="severity-pill @warning.SeverityName.ToLowerInvariant()">@warning.SeverityName</span>
                            <strong>@warning.WarningTypeName</strong>
                            @if (warning.IsDismissed) { <span class="location-badge">dismissed</span> }
                            @if (warning.IsIntentional) { <span class="location-badge">intentional</span> }
                        </div>
                        <p>@warning.Message</p>
                        @if (!string.IsNullOrWhiteSpace(warning.Details))
                        {
                            <p class="muted">@warning.Details</p>
                        }
                        <p class="muted">
                            @(warning.BookTitle ?? "Project")
                            @if (warning.SceneID.HasValue)
                            {
                                <span>/ Scene @warning.SceneNumber: @warning.SceneTitle</span>
                            }
                            <span>/ @warning.EntityType</span>
                            <span>/ Last seen @warning.LastDetectedDate.ToString("dd MMM yyyy HH:mm")</span>
                        </p>
                    </div>
                    <div class="warning-actions">
                        @if (warning.SceneID.HasValue)
                        {
                            <a class="btn btn-outline-primary btn-sm"
                               asp-controller="Timeline"
                               asp-action="Index"
                               asp-route-ProjectID="@Model.Project.ProjectID"
                               asp-route-BookID="@warning.BookID"
                               asp-route-SelectedSceneID="@warning.SceneID"
                               asp-route-FocusType="scene"
                               asp-route-FocusID="@warning.SceneID">Open scene</a>
                        }
                        @if (warning.IsDismissed || warning.IsIntentional)
                        {
                            <form asp-action="Restore" method="post">
                                <input type="hidden" name="id" value="@warning.ContinuityWarningID" />
                                <input type="hidden" name="projectId" value="@Model.Project.ProjectID" />
                                <input type="hidden" name="bookId" value="@Model.BookID" />
                                <input type="hidden" name="sceneId" value="@Model.SceneID" />
                                <button class="btn btn-outline-secondary btn-sm" type="submit">Restore</button>
                            </form>
                        }
                        else
                        {
                            <form asp-action="Dismiss" method="post">
                                <input type="hidden" name="id" value="@warning.ContinuityWarningID" />
                                <input type="hidden" name="projectId" value="@Model.Project.ProjectID" />
                                <input type="hidden" name="bookId" value="@Model.BookID" />
                                <input type="hidden" name="sceneId" value="@Model.SceneID" />
                                <button class="btn btn-outline-secondary btn-sm" type="submit">Dismiss</button>
                            </form>
                            <form asp-action="MarkIntentional" method="post">
                                <input type="hidden" name="id" value="@warning.ContinuityWarningID" />
                                <input type="hidden" name="projectId" value="@Model.Project.ProjectID" />
                                <input type="hidden" name="bookId" value="@Model.BookID" />
                                <input type="hidden" name="sceneId" value="@Model.SceneID" />
                                <button class="btn btn-outline-secondary btn-sm" type="submit">Intentional</button>
                            </form>
                        }
                    </div>
                </article>
            }
        </div>
    }
</section>
