using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
using Indotalent.Features.Purchase.PaymentDisburse.Cqrs;

namespace Indotalent.Features.Purchase.PaymentDisburse;

public class PaymentDisbursePdfGenerator
{
    public static byte[] Generate(GetPaymentDisburseByIdResponse data)
    {
        QuestPDF.Settings.License = LicenseType.Community;

        var primaryBlue = "#0D47A1";
        var textSlate = "#64748b";

        return Document.Create(container =>
        {
            container.Page(page =>
            {
                page.Size(PageSizes.A4);
                page.Margin(1.5f, Unit.Centimetre);
                page.PageColor(Colors.White);
                page.DefaultTextStyle(x => x.FontSize(9).FontFamily(Fonts.Verdana));

                page.Header().Row(row =>
                {
                    row.RelativeItem().Column(col =>
                    {
                        col.Item().Text("PAYMENT VOUCHER")
                            .FontSize(20).ExtraBold().FontColor(primaryBlue);
                        col.Item().Text($"{data.AutoNumber}").FontSize(11).SemiBold();
                    });

                    row.RelativeItem().AlignRight().Column(col =>
                    {
                        col.Item().Text(data.CompanyName).FontSize(11).ExtraBold();
                        col.Item().Text($"{data.CompanyStreet}").FontColor(textSlate);
                        col.Item().Text($"{data.CompanyCity}").FontColor(textSlate);
                    });
                });

                page.Content().PaddingVertical(20).Column(col =>
                {
                    col.Item().Row(row =>
                    {
                        row.RelativeItem().Column(c =>
                        {
                            c.Item().BorderBottom(1).PaddingBottom(5).Text("PAID TO (VENDOR)").FontSize(8).Bold().FontColor(textSlate);
                            c.Item().PaddingTop(5).Text(data.VendorName).Bold();
                            c.Item().Text(data.VendorStreet);
                            c.Item().Text(data.VendorCity);
                        });

                        row.ConstantItem(40);

                        row.RelativeItem().Column(c =>
                        {
                            c.Item().BorderBottom(1).PaddingBottom(5).Text("PAYMENT INFO").FontSize(8).Bold().FontColor(textSlate);
                            c.Item().PaddingTop(5).Row(r => {
                                r.RelativeItem().Text("Date");
                                r.RelativeItem().AlignRight().Text(data.PaymentDate?.ToString("dd MMM yyyy"));
                            });
                            c.Item().Row(r => {
                                r.RelativeItem().Text("Method");
                                r.RelativeItem().AlignRight().Text(data.PaymentMethodName);
                            });
                            c.Item().Row(r => {
                                r.RelativeItem().Text("Bill Ref");
                                r.RelativeItem().AlignRight().Text(data.BillNumber).Bold();
                            });
                        });
                    });

                    col.Item().PaddingTop(20).Table(table =>
                    {
                        table.ColumnsDefinition(columns =>
                        {
                            columns.ConstantColumn(25);
                            columns.RelativeColumn(6);
                            columns.RelativeColumn(3);
                        });

                        table.Header(header =>
                        {
                            header.Cell().Element(HeaderStyle).Text("#");
                            header.Cell().Element(HeaderStyle).Text("Item Details");
                            header.Cell().Element(HeaderStyle).AlignRight().Text($"Amount ({data.CurrencySymbol})");

                            static IContainer HeaderStyle(IContainer container) => container.DefaultTextStyle(x => x.SemiBold().FontColor(Colors.White)).PaddingVertical(5).PaddingHorizontal(5).Background("#0D47A1");
                        });

                        var index = 1;
                        foreach (var item in data.Items)
                        {
                            table.Cell().Element(CellStyle).Text($"{index++}");
                            table.Cell().Element(CellStyle).Column(c => {
                                c.Item().Text(item.ProductName).Bold();
                                if (!string.IsNullOrEmpty(item.Summary)) c.Item().Text(item.Summary).FontSize(8).FontColor(textSlate);
                            });
                            table.Cell().Element(CellStyle).AlignRight().Text(item.Total.ToString("N2"));

                            static IContainer CellStyle(IContainer container) => container.BorderBottom(1).BorderColor("#E2E8F0").PaddingVertical(8).PaddingHorizontal(5);
                        }
                    });

                    col.Item().PaddingTop(10).Row(row =>
                    {
                        row.RelativeItem().Column(c => {
                            c.Item().Text("Description:").FontSize(8).Bold();
                            c.Item().Text(string.IsNullOrEmpty(data.Description) ? "-" : data.Description).FontSize(8);
                        });
                        row.ConstantItem(200).Column(c =>
                        {
                            c.Item().Row(r => { r.RelativeItem().Text("Sub Total"); r.RelativeItem().AlignRight().Text(data.PurchaseOrderBeforeTaxAmount.ToString("N2")); });
                            c.Item().Row(r => { r.RelativeItem().Text("Tax Amount"); r.RelativeItem().AlignRight().Text(data.PurchaseOrderTaxAmount.ToString("N2")); });
                            c.Item().PaddingTop(5).BorderTop(1).Row(r => { r.RelativeItem().Text($"TOTAL BILL").Bold(); r.RelativeItem().AlignRight().Text(data.PurchaseOrderAfterTaxAmount.ToString("N2")).ExtraBold(); });
                            c.Item().PaddingTop(5).Background("#F1F5F9").PaddingHorizontal(5).Row(r => { r.RelativeItem().Text($"DISBURSED").Bold().FontColor(primaryBlue); r.RelativeItem().AlignRight().Text(data.PaymentAmount.ToString("N2")).ExtraBold().FontColor(primaryBlue); });
                        });
                    });
                });

                page.Footer().AlignCenter().Text(x => { x.Span("Page ").FontSize(8); x.CurrentPageNumber().FontSize(8); });
            });
        }).GeneratePdf();
    }
}