@model FeatureRequestIndexViewModel
@using Microsoft.AspNetCore.Html
@using System.Text.Encodings.Web
@{
    ViewData["Title"] = "Feature Requests";
}

<div class="page-heading compact">
    <div>
        <p class="eyebrow">Feedback</p>
        <h1>Feature Requests</h1>
        <p class="lead-text">Send suggestions and keep track of your conversations with the PlotDirector team.</p>
    </div>
    <div class="button-row">
        <a class="btn btn-outline-primary" asp-action="Roadmap">View Roadmap</a>
        <a class="btn btn-primary" asp-action="Create">Submit Feature Request</a>
    </div>
</div>

<nav class="mb-3" aria-label="Breadcrumb">
    <a asp-controller="Projects" asp-action="Index">Projects</a>
    <span class="muted"> / Feature Requests</span>
</nav>

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

@if (!Model.Requests.Any())
{
    <section class="empty-panel">
        <h2>No suggestions yet</h2>
        <p>No suggestions yet. If something keeps getting in your way, or you have an idea that would make PlotDirector better, send it in.</p>
        <a class="btn btn-primary" asp-action="Create">Submit Feature Request</a>
    </section>
}
else
{
    <section class="edit-panel">
        <div class="table-responsive">
            <table class="table table-sm align-middle">
                <thead>
                    <tr>
                        <th>Title</th>
                        <th>Status</th>
                        <th>Latest Activity</th>
                        <th>Conversation</th>
                        <th>App Area</th>
                        <th>Importance</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var request in Model.Requests)
                    {
                        <tr>
                            <td>
                                @if (request.UserNeedsAttention)
                                {
                                    <span class="badge text-bg-warning me-1">New</span>
                                }
                                @request.Title
                            </td>
                            <td>@StatusBadge(request.Status)</td>
                            <td>@FormatNullableDate(request.LatestActivityUtc)</td>
                            <td>@ConversationBadge(request.ConversationState)</td>
                            <td>@(request.AppArea ?? "Not set")</td>
                            <td>@(request.Importance ?? "Not set")</td>
                            <td class="text-end">
                                <a class="btn btn-outline-primary btn-sm" asp-action="Details" asp-route-id="@request.FeatureRequestID">Open</a>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
    </section>
}

@functions {
    private static string FormatDate(DateTime value) => value.ToLocalTime().ToString("yyyy-MM-dd");

    private static string FormatNullableDate(DateTime? value) => value.HasValue ? FormatDate(value.Value) : "None yet";

    private static IHtmlContent StatusBadge(string status)
    {
        var css = status switch
        {
            FeatureRequestStatuses.New => "text-bg-primary",
            FeatureRequestStatuses.NeedsDetail => "text-bg-warning",
            FeatureRequestStatuses.UnderReview => "text-bg-info",
            FeatureRequestStatuses.Candidate => "text-bg-secondary",
            FeatureRequestStatuses.Planned => "text-bg-success",
            FeatureRequestStatuses.InProgress => "text-bg-success",
            FeatureRequestStatuses.Shipped => "text-bg-dark",
            FeatureRequestStatuses.NotPlanned => "text-bg-light",
            _ => "text-bg-secondary"
        };

        return new HtmlString($"<span class=\"badge {css}\">{HtmlEncoder.Default.Encode(status)}</span>");
    }

    private static IHtmlContent ConversationBadge(string state)
    {
        var css = state switch
        {
            FeatureRequestConversationStates.WaitingForAdminReply => "text-bg-info",
            FeatureRequestConversationStates.WaitingForUserReply => "text-bg-warning",
            _ => "text-bg-secondary"
        };

        return new HtmlString($"<span class=\"badge {css}\">{HtmlEncoder.Default.Encode(state)}</span>");
    }
}
