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

namespace DocumentOperator.Application.ValidatePdfA.Queries;

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

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

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