using AutoMapper;
using DocumentOperator.Application.Common.DTOs;
using DocumentOperator.Application.Common.Interfaces;
using MediatR;
namespace DocumentOperator.Application.CheckPdfAttachments.Queries;
///
/// Query for checking PDF attachments (supports both byte array and Base64 input)
///
public record CheckPdfAttachmentsQuery : IRequest
{
///
/// PDF as byte array (direct upload via multipart/form-data)
///
public byte[]? PdfBytes { get; init; }
///
/// PDF as Base64 string (for API clients using application/json)
///
public string? Base64Pdf { get; init; }
}
///
/// Handler for CheckPdfAttachmentsQuery
/// Orchestrates PDF attachment checking using IPdfProcessor and AutoMapper
///
public class CheckPdfAttachmentsQueryHandler(IPdfProcessor PdfProcessor, IMapper Mapper)
: IRequestHandler
{
///
/// Checks PDF attachments and returns detailed metadata
///
public async Task Handle(CheckPdfAttachmentsQuery request, CancellationToken cancellationToken)
{
// Use byte[] if available, otherwise convert Base64
byte[] pdfBytes = request.PdfBytes ?? Convert.FromBase64String(request.Base64Pdf!);
// Convert to stream for IPdfProcessor
using var pdfStream = new MemoryStream(pdfBytes);
// Call DevExpress service (exceptions propagate naturally)
var attachmentInfo = await PdfProcessor.CheckAttachmentsAsync(pdfStream);
// Map DTO to response DTO using AutoMapper
return Mapper.Map(attachmentInfo);
}
}