using Baya.Application.Common;
using Baya.Application.Contracts.Audit;
using Baya.Application.Models.Audit;
using Baya.Application.Models.Common;
using Mediator;

namespace Baya.Application.Features.Audit.Queries.GetAuditTrail;

internal sealed class GetAuditTrailQueryHandler(IAuditLogger auditLogger)
    : IRequestHandler<GetAuditTrailQuery, OperationResult<PagedResult<AuditLogDto>>>
{
    public async ValueTask<OperationResult<PagedResult<AuditLogDto>>> Handle(GetAuditTrailQuery request, CancellationToken cancellationToken)
    {
        var (page, pageSize) = Pagination.Normalize(request.Page, request.PageSize);
        var result = await auditLogger.GetTrailAsync(
            request.EntityType, request.EntityId, request.ActorId, request.Action, request.From, request.To,
            page, pageSize, cancellationToken);
        return OperationResult<PagedResult<AuditLogDto>>.SuccessResult(result);
    }
}
