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

@functions {
    private static string SeverityBadgeClass(string severity) => severity switch
    {
        "Critical" or "Error" => "bg-danger",
        "Warning" => "bg-warning text-dark",
        _ => "bg-info text-dark"
    };
}

<nav class="breadcrumb-trail" aria-label="Breadcrumb">
    <a asp-controller="Projects" asp-action="Index">Projects</a>
    <a asp-controller="Projects" asp-action="Details" asp-route-id="@Model.Project.ProjectID">@Model.Project.ProjectName</a>
    <span>Warnings</span>
</nav>

<partial name="_ProjectSectionNav" model="Model.Project" />

<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">Chapter</label>
            <select name="chapterId" asp-items="Model.ChapterOptions" class="form-select form-select-sm">
                <option value="">All chapters</option>
            </select>
        </div>
        <div class="col-md-2">
            <label class="form-label">Category</label>
            <select name="category" asp-items="Model.CategoryOptions" class="form-select form-select-sm">
                <option value="">All categories</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>
            <label class="form-check">
                <input class="form-check-input" name="showAcknowledgedWarnings" type="checkbox" value="true" checked="@Model.ShowAcknowledgedWarnings" />
                <span class="form-check-label">Acknowledged</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 <help-icon key="warnings.severitySummary" /></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 <help-icon key="warnings.typeSummary" /></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 <help-icon key="warnings.list" /></h2>
    @if (!Model.ProjectWarnings.Any())
    {
        <p class="muted">@(Model.HasAnyWarnings ? "No warnings match the current filters." : "No project warnings detected.")</p>
    }
    else
    {
        <div class="accordion" id="warning-severity-groups">
            @foreach (var group in Model.WarningGroups)
            {
                var groupId = $"warning-group-{group.Severity.ToLowerInvariant().Replace(" ", "-")}";
                <div class="accordion-item">
                    <h3 class="accordion-header" id="@($"{groupId}-heading")">
                        <button class="accordion-button @(group.IsExpanded ? "" : "collapsed")" type="button" data-bs-toggle="collapse" data-bs-target="#@groupId" aria-expanded="@group.IsExpanded.ToString().ToLowerInvariant()" aria-controls="@groupId">
                            @group.Severity (@group.Warnings.Count)
                        </button>
                    </h3>
                    <div id="@groupId" class="accordion-collapse collapse @(group.IsExpanded ? "show" : "")" aria-labelledby="@($"{groupId}-heading")">
                        <div class="accordion-body p-0">
                            <div class="table-responsive">
                                <table class="table table-sm align-middle mb-0">
                                    <thead>
                                        <tr>
                                            <th>Severity</th>
                                            <th>Category</th>
                                            <th>Warning Type</th>
                                            <th>Message</th>
                                            <th>Scene</th>
                                            <th>Status</th>
                                            <th>Actions</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach (var warning in group.Warnings)
                                        {
                                            <tr>
                                                <td><span class="badge @SeverityBadgeClass(warning.Severity)">@warning.Severity</span></td>
                                                <td>@warning.Category</td>
                                                <td>@warning.WarningTypeDisplayName</td>
                                                <td>
                                                    <div>@warning.Message</div>
                                                    @if (!string.IsNullOrWhiteSpace(warning.Details))
                                                    {
                                                        <small class="text-muted d-block">@warning.Details</small>
                                                    }
                                                    @if (!string.IsNullOrWhiteSpace(warning.PreviousKnowledgeState) && !string.IsNullOrWhiteSpace(warning.CurrentKnowledgeState))
                                                    {
                                                        <small class="text-muted d-block">@warning.PreviousKnowledgeState &rarr; @warning.CurrentKnowledgeState</small>
                                                    }
                                                    @if (warning.IsAcknowledged && !string.IsNullOrWhiteSpace(warning.AcknowledgementNotes))
                                                    {
                                                        <small class="text-muted d-block">Note: @warning.AcknowledgementNotes</small>
                                                    }
                                                </td>
                                                <td>
                                                    @if (warning.PreviousSceneId.HasValue || warning.CurrentSceneId.HasValue)
                                                    {
                                                        @if (warning.PreviousSceneId.HasValue)
                                                        {
                                                            <small class="text-muted d-block">Previous</small>
                                                            <a class="d-block mb-2" asp-controller="Timeline" asp-action="Index" asp-route-projectId="@Model.Project.ProjectID" asp-route-selectedSceneId="@warning.PreviousSceneId">@warning.PreviousContextSceneDisplayName</a>
                                                        }
                                                        @if (warning.CurrentSceneId.HasValue)
                                                        {
                                                            <small class="text-muted d-block">Current</small>
                                                            <a class="d-block" asp-controller="Timeline" asp-action="Index" asp-route-projectId="@Model.Project.ProjectID" asp-route-selectedSceneId="@warning.CurrentSceneId">@warning.CurrentContextSceneDisplayName</a>
                                                        }
                                                    }
                                                    else if (warning.SceneID.HasValue)
                                                    {
                                                        <a asp-controller="Timeline" asp-action="Index" asp-route-projectId="@Model.Project.ProjectID" asp-route-selectedSceneId="@warning.SceneID">@warning.ContextSceneDisplayName</a>
                                                    }
                                                    else
                                                    {
                                                        <span class="muted">Project-wide</span>
                                                    }
                                                </td>
                                                <td>
                                                    @if (warning.IsDismissed)
                                                    {
                                                        <span class="badge bg-secondary">Dismissed</span>
                                                    }
                                                    else if (warning.IsIntentional || warning.IsAcknowledged)
                                                    {
                                                        <span class="badge bg-secondary">Acknowledged</span>
                                                    }
                                                    else
                                                    {
                                                        <span class="badge bg-warning text-dark">Active</span>
                                                    }
                                                </td>
                                                <td>
                                                    <div class="d-grid gap-2" style="min-width: 150px;">
                                                        @if (warning.CanInvestigate)
                                                        {
                                                            <a class="btn btn-outline-primary btn-sm w-100"
                                                               asp-controller="ContinuityExplorer"
                                                               asp-action="Index"
                                                               asp-route-projectId="@Model.Project.ProjectID"
                                                               asp-route-mode="@warning.InvestigationDisplayMode"
                                                               asp-route-characterId="@warning.CharacterID"
                                                               asp-route-assetId="@warning.AssetID"
                                                               asp-route-sceneId="@warning.InvestigationSceneId">Investigate</a>
                                                        }
                                                        @if (warning.Source == "Persisted" && warning.ContinuityWarningID.HasValue)
                                                        {
                                                            @if (warning.IsDismissed || warning.IsIntentional)
                                                            {
                                                                <form asp-action="Restore" method="post" class="d-grid">
                                                                    <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 w-100" type="submit">Restore</button>
                                                                </form>
                                                            }
                                                            else
                                                            {
                                                                <form asp-action="Dismiss" method="post" class="d-grid">
                                                                    <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 w-100" type="submit">Dismiss</button>
                                                                </form>
                                                                <form asp-action="MarkIntentional" method="post" class="d-grid">
                                                                    <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 w-100" type="submit">Intentional</button>
                                                                </form>
                                                            }
                                                        }
                                                        else if (warning.IsAcknowledged)
                                                        {
                                                            <span class="badge bg-secondary justify-self-stretch">Acknowledged</span>
                                                            <form asp-action="RestoreGeneratedWarning" method="post" class="d-grid">
                                                                <input type="hidden" name="WarningType" value="@warning.WarningType" />
                                                                <input type="hidden" name="SceneID" value="@warning.SceneID" />
                                                                <input type="hidden" name="CharacterID" value="@warning.CharacterID" />
                                                                <input type="hidden" name="AssetID" value="@warning.AssetID" />
                                                                <input type="hidden" name="ReturnFilter.ProjectID" value="@Model.Project.ProjectID" />
                                                                <input type="hidden" name="ReturnFilter.BookID" value="@Model.BookID" />
                                                                <input type="hidden" name="ReturnFilter.ChapterID" value="@Model.ChapterID" />
                                                                <input type="hidden" name="ReturnFilter.SceneID" value="@Model.SceneID" />
                                                                <input type="hidden" name="ReturnFilter.WarningTypeID" value="@Model.WarningTypeID" />
                                                                <input type="hidden" name="ReturnFilter.WarningSeverityID" value="@Model.WarningSeverityID" />
                                                                <input type="hidden" name="ReturnFilter.EntityType" value="@Model.EntityType" />
                                                                <input type="hidden" name="ReturnFilter.Category" value="@Model.Category" />
                                                                <input type="hidden" name="ReturnFilter.IncludeDismissed" value="@Model.IncludeDismissed" />
                                                                <input type="hidden" name="ReturnFilter.IncludeIntentional" value="@Model.IncludeIntentional" />
                                                                <input type="hidden" name="ReturnFilter.ShowAcknowledgedWarnings" value="@Model.ShowAcknowledgedWarnings" />
                                                                <button class="btn btn-outline-secondary btn-sm w-100" type="submit">Restore</button>
                                                            </form>
                                                        }
                                                        else
                                                        {
                                                            <form asp-action="AcknowledgeGeneratedWarning" method="post" class="d-grid gap-2">
                                                                <input type="hidden" name="WarningType" value="@warning.WarningType" />
                                                                <input type="hidden" name="SceneID" value="@warning.SceneID" />
                                                                <input type="hidden" name="CharacterID" value="@warning.CharacterID" />
                                                                <input type="hidden" name="AssetID" value="@warning.AssetID" />
                                                                <input type="hidden" name="ReturnFilter.ProjectID" value="@Model.Project.ProjectID" />
                                                                <input type="hidden" name="ReturnFilter.BookID" value="@Model.BookID" />
                                                                <input type="hidden" name="ReturnFilter.ChapterID" value="@Model.ChapterID" />
                                                                <input type="hidden" name="ReturnFilter.SceneID" value="@Model.SceneID" />
                                                                <input type="hidden" name="ReturnFilter.WarningTypeID" value="@Model.WarningTypeID" />
                                                                <input type="hidden" name="ReturnFilter.WarningSeverityID" value="@Model.WarningSeverityID" />
                                                                <input type="hidden" name="ReturnFilter.EntityType" value="@Model.EntityType" />
                                                                <input type="hidden" name="ReturnFilter.Category" value="@Model.Category" />
                                                                <input type="hidden" name="ReturnFilter.IncludeDismissed" value="@Model.IncludeDismissed" />
                                                                <input type="hidden" name="ReturnFilter.IncludeIntentional" value="@Model.IncludeIntentional" />
                                                                <input type="hidden" name="ReturnFilter.ShowAcknowledgedWarnings" value="@Model.ShowAcknowledgedWarnings" />
                                                                <label class="form-label mb-0 small">Reason (optional)</label>
                                                                <input class="form-control form-control-sm" type="text" name="Notes" maxlength="500" />
                                                                <button class="btn btn-outline-success btn-sm w-100" type="submit">Acknowledge</button>
                                                            </form>
                                                        }
                                                    </div>
                                                </td>
                                            </tr>
                                        }
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            }
        </div>
    }
</section>
