using IWS5.Importer.Abstractions.FileSystem;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Drawing;

namespace IWS5.Importer.Worker.Extensions;

public class ErrorReportPdfPageGenerator : IPdfPageGenerator<List<(string filePath, string reason)>>
{
    public string Generate(List<(string filePath, string reason)> errors)
    {
        using var document = new PdfDocument();
        document.PageSettings.Size = PdfPageSize.A4;

        PdfFont titleFont = new PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold);
        PdfFont lineFont = new PdfStandardFont(PdfFontFamily.Helvetica, 11);

        float margin = 10f;
        float lineHeight = 16f;
        float y = margin;

        PdfPage page = document.Pages.Add();
        PdfGraphics graphics = page.Graphics;

        // Title
        graphics.DrawString("❌ Error Report", titleFont, PdfBrushes.DarkRed, new PointF(margin, y));
        y += 25f;

        foreach (var (filePath, reason) in errors)
        {
            if (y > page.GetClientSize().Height - lineHeight)
            {
                page = document.Pages.Add();
                graphics = page.Graphics;
                y = margin;
            }

            string line = $"File: {filePath} | Reason: {reason}";
            graphics.DrawString(line, lineFont, PdfBrushes.Black, new PointF(margin, y));
            y += lineHeight;
        }

        using var memoryStream = new MemoryStream();
        document.Save(memoryStream);
        document.Close(true);

        return Convert.ToBase64String(memoryStream.ToArray());
    }
}
