using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace SelectPdf.Samples
{
    // Minimal-API handlers for the /ViewHttpPostData inspector. The page is
    // reached by the SelectPdf converter as an external HTTP POST when
    // running the HttpPostRequest sample, so it cannot live behind Blazor's
    // antiforgery middleware. Returns a self-contained HTML document so the
    // converter sees what it needs without depending on the Blazor shell.
    public static class ViewHelpers
    {
        public static IResult ViewHttpPostData(HttpRequest request)
        {
            StringBuilder output = new StringBuilder();
            output.Append("<br/>Request method: " + request.Method + "<br/>");

            if (HttpMethods.IsPost(request.Method) && request.HasFormContentType)
            {
                foreach (var element in request.Form)
                {
                    output.Append("Name: " + element.Key + "<br/>");
                    foreach (var value in element.Value)
                    {
                        output.Append("Value: " + value + "<br/>");
                    }
                    output.Append("<br/>");
                }
            }

            string body = output.ToString();
            string html = $@"<!DOCTYPE html>
<html lang=""en-US"">
<head>
    <meta charset=""UTF-8"">
    <title>SelectPdf for .NET 10 - Helper Page - View HTTP Posted Parameters</title>
</head>
<body>
    <article>
        <h1>SelectPdf for .NET 10 - Helper Page - View Posted Parameters - C# / .NET 10 Sample</h1>
        <p>This page shows the HTTP POST variables sent to it:<br/>{body}</p>
    </article>
</body>
</html>";

            return Results.Content(html, "text/html");
        }
    }
}
