@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">Feature Request</p>
        <h1>@request.Title</h1>
    </div>
    <div class="button-row">
        <a class="btn btn-outline-secondary" asp-action="Index">Back to requests</a>
    </div>
</div>

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

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

<section class="edit-panel">
    <h2>Conversation</h2>
    @await Html.PartialAsync("_Conversation", Model)
    <form asp-action="Reply" 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>

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

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

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