using AutoMapper;
using DocumentOperator.Application.Common.DTOs;
using DocumentOperator.Application.Common.Interfaces;
using MediatR;

namespace DocumentOperator.Application.ValidatePdf.Queries;

/// <summary>
/// Query for PDF validation (Stream-based)
/// </summary>
public record ValidatePdfQuery : IRequest<PdfValidationResult>
{
    /// <summary>
    /// PDF as stream (caller is responsible for disposal)
    /// </summary>
    public required Stream PdfStream { get; init; }
}

/// <summary>
/// Handler for ValidatePdfQuery
/// Orchestrates PDF validation using IPdfProcessor and AutoMapper
/// </summary>
public class ValidatePdfQueryHandler(IPdfProcessor PdfProcessor, IMapper Mapper) 
    : IRequestHandler<ValidatePdfQuery, PdfValidationResult>
{
    /// <summary>
    /// Validates PDF and returns metadata
    /// </summary>
    public async Task<PdfValidationResult> Handle(ValidatePdfQuery request, CancellationToken cancellationToken)
    {
        // Call DevExpress service directly with stream (exceptions propagate naturally)
        var metadata = await PdfProcessor.ValidateAsync(request.PdfStream);

        // Map DTO to response DTO using AutoMapper
        return Mapper.Map<PdfValidationResult>(metadata);
    }
}
