@model FeatureRequestDetailsViewModel
@using Microsoft.AspNetCore.Html
@using System.Text.Encodings.Web
@{
    ViewData["Title"] = Model.Request.Title;
    var request = Model.Request;
}

<div class="page-heading compact">
    <div>
        <p class="eyebrow">Admin / Feature Request</p>
        <h1>@request.Title</h1>
        <p class="lead-text">@request.UserDisplayName (@request.UserEmail)</p>
    </div>
    <div class="button-row">
        <a class="btn btn-outline-secondary" asp-action="FeatureRequests">Back to list</a>
    </div>
</div>

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

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

<div class="row g-3">
    <div class="col-lg-8">
        <section class="edit-panel">
            <h2>Request</h2>
            <div class="row g-3 mb-3">
                <div class="col-md-4">
                    <p class="eyebrow">Status</p>
                    <p>@StatusBadge(request.Status)</p>
                </div>
                <div class="col-md-4">
                    <p class="eyebrow">Conversation</p>
                    <p>@ConversationBadge(request.ConversationState)</p>
                </div>
                <div class="col-md-4">
                    <p class="eyebrow">App area</p>
                    <p>@(request.AppArea ?? "Not set")</p>
                </div>
                <div class="col-md-4">
                    <p class="eyebrow">Importance</p>
                    <p>@(request.Importance ?? "Not set")</p>
                </div>
                <div class="col-md-4">
                    <p class="eyebrow">Created</p>
                    <p>@FormatDate(request.CreatedUtc)</p>
                </div>
                <div class="col-md-4">
                    <p class="eyebrow">Latest activity</p>
                    <p>@FormatNullableDate(request.LatestActivityUtc)</p>
                </div>
            </div>
            <p style="white-space: pre-wrap">@request.Description</p>
        </section>

        <section class="edit-panel">
            <h2>Conversation</h2>
            @await Html.PartialAsync("~/Views/FeatureRequests/_Conversation.cshtml", Model)
            <form asp-action="FeatureRequestReply" method="post" class="mt-3">
                <input type="hidden" asp-for="Reply.FeatureRequestID" />
                <div class="mb-3">
                    <label class="form-label" asp-for="Reply.MessageBody">Reply</label>
                    <textarea class="form-control" asp-for="Reply.MessageBody" rows="4"></textarea>
                    <span class="text-danger" asp-validation-for="Reply.MessageBody"></span>
                </div>
                <button class="btn btn-primary" type="submit">Send reply</button>
            </form>
        </section>
    </div>

    <div class="col-lg-4">
        <section class="edit-panel">
            <h2>Admin controls</h2>
            <form asp-action="UpdateFeatureRequest" method="post">
                <input type="hidden" name="FeatureRequestID" value="@request.FeatureRequestID" />
                <div class="mb-3">
                    <label class="form-label" for="Status">Status</label>
                    <select class="form-select" id="Status" name="Status">
                        @foreach (var status in Model.StatusOptions)
                        {
                            <option value="@status" selected="@(string.Equals(request.Status, status, StringComparison.OrdinalIgnoreCase))">@status</option>
                        }
                    </select>
                </div>
                <div class="form-check mb-3">
                    <input class="form-check-input" type="checkbox" id="IsPublicCandidate" name="IsPublicCandidate" value="true" checked="@request.IsPublicCandidate" />
                    <input type="hidden" name="IsPublicCandidate" value="false" />
                    <label class="form-check-label" for="IsPublicCandidate">Show on curated roadmap</label>
                    <div class="form-text">Only Candidate, Planned, In Progress and Shipped requests appear on the roadmap.</div>
                </div>
                <div class="mb-3">
                    <label class="form-label" for="AdminNotes">Admin notes</label>
                    <textarea class="form-control" id="AdminNotes" name="AdminNotes" rows="8">@request.AdminNotes</textarea>
                </div>
                <button class="btn btn-primary" type="submit">Save</button>
            </form>
        </section>
    </div>
</div>

@section Scripts {
    <partial name="_ValidationScriptsPartial" />
}

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

    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-danger",
            FeatureRequestConversationStates.WaitingForUserReply => "text-bg-warning",
            _ => "text-bg-secondary"
        };

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