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

namespace SelectPdf.Samples
{
    // Minimal-API handlers for the HTTP-inspection helper pages
    // (/ViewHttpPostData, /ViewHttpHeaders, /ViewHttpCookies). Each is
    // reached by the SelectPdf converter as an external HTTP request when
    // running the HttpPostRequest / HttpHeaders / HttpCookies samples, so
    // they cannot live behind Blazor's antiforgery middleware. They also must
    // NOT be interactive Blazor (@page) components: an InteractiveServer page
    // prerenders with the converter's request headers/cookies, then the
    // interactive circuit re-renders it over a websocket WITHOUT those
    // request-scoped values, wiping them from the DOM the converter captures.
    // Returning a self-contained, statically-rendered HTML document is the
    // only way the converter reliably sees the headers/cookies it sent.
    //
    // The page body is the same .entry-content article the live Blazor pages
    // render, wrapped by SamplePageShell in the full SelectPdf.Web design-system
    // document (spdf-nav + styled content + slim footer + assets/css/spdf.css)
    // so the generated PDF is fully styled, not bare HTML.
    public static class ViewHelpers
    {
        // Include PathBase so the shell's <base href> (and thus the relative
        // assets/css/spdf.css the converter must load) resolves under the
        // app's hosted path — "" at the site root, "/demo" when deployed as an
        // IIS sub-application. Without it the converter fetches the stylesheets
        // from the site root and the PDF comes out unstyled.
        private static string BaseUrl(HttpRequest request) =>
            $"{request.Scheme}://{request.Host}{request.PathBase}";

        // Builds the .entry-content article shared by all three helper pages.
        private static string Article(string heading, string lead, string body) => $@"
<article class=""post type-post status-publish format-standard hentry"">
    <header class=""entry-header"">
        <h1 class=""entry-title"">{heading}</h1>
    </header>
    <div class=""entry-content"">
        <p>
            {lead}
            <br />
            {body}
        </p>
    </div>
</article>";

        public static IResult ViewHttpPostData(HttpRequest request)
        {
            StringBuilder output = new StringBuilder();
            output.Append("Request method: " + request.Method + "<br/>");

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

            string article = Article(
                "SelectPdf for .NET 10 - Helper Page - View Posted Parameters - C# / .NET 10 Sample",
                "This page shows the HTTP POST variables sent to it:",
                output.ToString());

            return Results.Content(SamplePageShell.Wrap(article, BaseUrl(request)), "text/html");
        }

        public static IResult ViewHttpHeaders(HttpRequest request)
        {
            StringBuilder output = new StringBuilder();
            foreach (var header in request.Headers)
            {
                output.Append(header.Key + ": " + header.Value + "<br/>");
            }

            string article = Article(
                "SelectPdf for .NET 10 - Helper Page - View HTTP Headers - C# / .NET 10 Sample",
                "This page shows the HTTP Headers sent to it:",
                output.ToString());

            return Results.Content(SamplePageShell.Wrap(article, BaseUrl(request)), "text/html");
        }

        public static IResult ViewHttpCookies(HttpRequest request)
        {
            StringBuilder output = new StringBuilder();
            foreach (string key in request.Cookies.Keys)
            {
                output.Append(key + ": " + request.Cookies[key] + "<br/>");
            }

            string article = Article(
                "SelectPdf for .NET 10 - Helper Page - View HTTP Cookies - C# / .NET 10 Sample",
                "This page shows the HTTP Cookies sent to it:",
                output.ToString());

            return Results.Content(SamplePageShell.Wrap(article, BaseUrl(request)), "text/html");
        }
    }
}
