namespace DXApp.TemplateKitProject.Services
{
    using DevExpress.Pdf;
    using System.Text;

    public class ZugferdExtractorService
    {
        private static readonly string[] KnownFileNames =
        [
            "zugferd-invoice.xml",
        "factur-x.xml",
        "xrechnung.xml"
        ];

        public string? ExtractXml(Stream pdfStream)
        {
            using var processor = new PdfDocumentProcessor();
            processor.LoadDocument(pdfStream);

            foreach (var attachment in processor.Document.FileAttachments)
            {
                bool isZugferd = KnownFileNames.Any(name =>
                    attachment.FileName.Equals(name, StringComparison.OrdinalIgnoreCase));

                if (isZugferd || attachment.MimeType == "text/xml")
                {
                    byte[] data = attachment.Data;
                    // BOM entfernen falls vorhanden (EF BB BF am Anfang)
                    var text = Encoding.UTF8.GetString(data);
                    return text.TrimStart('\uFEFF');
                }
            }

            return null; // Kein ZUGFeRD-Anhang gefunden
        }
    }
}