using MediatR;
using Microsoft.Extensions.Logging;

namespace Application.Behaviors;

public sealed class LoggingBehavior<TRequest, TResponse>(ILogger<LoggingBehavior<TRequest, TResponse>> logger) : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull
{
    public async Task<TResponse> Handle(
        TRequest request,
        RequestHandlerDelegate<TResponse> next,
        CancellationToken ct
    ) {
        var requestName = typeof(TRequest).Name;

        logger.LogInformation("[Start] {Request}", requestName);

        var result = await next(ct);

        logger.LogInformation("[End] {Request}", requestName);

        return result;
    }
}