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

namespace DocumentOperator.Application.CheckPdfAttachments.Queries;

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

/// <summary>
/// Handler for CheckPdfAttachmentsQuery
/// Orchestrates PDF attachment checking using IPdfProcessor and AutoMapper
/// </summary>
public class CheckPdfAttachmentsQueryHandler(IPdfProcessor PdfProcessor, IMapper Mapper)
    : IRequestHandler<CheckPdfAttachmentsQuery, AttachmentCheckResult>
{
    /// <summary>
    /// Checks PDF attachments and returns detailed metadata
    /// </summary>
    public async Task<AttachmentCheckResult> Handle(CheckPdfAttachmentsQuery request, CancellationToken cancellationToken)
    {
        // Call DevExpress service directly with stream (exceptions propagate naturally)
        var attachmentInfo = await PdfProcessor.CheckAttachmentsAsync(request.PdfStream);

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