namespace DiunaBI.UI.Shared.Handlers;

public class HttpLoggingHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request,
        CancellationToken cancellationToken)
    {
        Console.WriteLine($"🌐 HTTP Request: {request.Method} {request.RequestUri}");
        Console.WriteLine($"   BaseAddress: {request.RequestUri?.GetLeftPart(UriPartial.Authority)}");
        Console.WriteLine($"   Path: {request.RequestUri?.PathAndQuery}");

        if (request.Headers.Authorization != null)
        {
            Console.WriteLine($"   Auth: {request.Headers.Authorization.Scheme} {request.Headers.Authorization.Parameter?[..Math.Min(20, request.Headers.Authorization.Parameter?.Length ?? 0)]}...");
        }
        else
        {
            Console.WriteLine($"   Auth: None");
        }

        var response = await base.SendAsync(request, cancellationToken);

        Console.WriteLine($"   Response: {(int)response.StatusCode} {response.StatusCode}");

        return response;
    }
}
