using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
using SportsDivision.Application.DTOs.ReportDtos;

namespace SportsDivision.Web.Reports;

public class RegistrationByGenderDocument : IDocument
{
    private readonly IEnumerable<RegistrationByGenderReportDto> _data;
    private readonly string _tournamentName;

    private static readonly string HeaderColor = "#003366";
    private static readonly Color AltRowColor = Color.FromHex("#F5F5F5");
    private static readonly string ReportTitle = "Registration by Gender Report";

    public RegistrationByGenderDocument(IEnumerable<RegistrationByGenderReportDto> data, string tournamentName)
    {
        _data = data;
        _tournamentName = tournamentName;
    }

    public DocumentMetadata GetMetadata() => DocumentMetadata.Default;

    public void Compose(IDocumentContainer container)
    {
        container.Page(page =>
        {
            page.Size(PageSizes.A4);
            page.Margin(30);

            page.Header().Element(ComposeHeader);
            page.Content().Element(ComposeContent);
            page.Footer().Element(ComposeFooter);
        });
    }

    private void ComposeHeader(IContainer container)
    {
        container.Column(column =>
        {
            column.Item().AlignCenter().Text("Dominica Sports Division")
                .FontSize(18).Bold().FontColor(HeaderColor);

            column.Item().AlignCenter().Text(_tournamentName)
                .FontSize(14).SemiBold();

            column.Item().AlignCenter().Text(ReportTitle)
                .FontSize(12);

            column.Item().PaddingVertical(5).LineHorizontal(1).LineColor(HeaderColor);
        });
    }

    private void ComposeContent(IContainer container)
    {
        container.PaddingVertical(10).Table(table =>
        {
            table.ColumnsDefinition(columns =>
            {
                columns.RelativeColumn(3);  // Event
                columns.RelativeColumn(2);  // Level
                columns.ConstantColumn(60); // Male
                columns.ConstantColumn(60); // Female
                columns.ConstantColumn(60); // Total
            });

            table.Header(header =>
            {
                header.Cell().Background(HeaderColor).Padding(5)
                    .Text("Event").FontColor(Colors.White).FontSize(10).Bold();
                header.Cell().Background(HeaderColor).Padding(5)
                    .Text("Level").FontColor(Colors.White).FontSize(10).Bold();
                header.Cell().Background(HeaderColor).Padding(5).AlignRight()
                    .Text("Male").FontColor(Colors.White).FontSize(10).Bold();
                header.Cell().Background(HeaderColor).Padding(5).AlignRight()
                    .Text("Female").FontColor(Colors.White).FontSize(10).Bold();
                header.Cell().Background(HeaderColor).Padding(5).AlignRight()
                    .Text("Total").FontColor(Colors.White).FontSize(10).Bold();
            });

            var rowIndex = 0;
            foreach (var item in _data)
            {
                var bgColor = rowIndex % 2 == 0 ? Colors.White : AltRowColor;

                table.Cell().Background(bgColor).Padding(5)
                    .Text(item.EventName).FontSize(10);
                table.Cell().Background(bgColor).Padding(5)
                    .Text(item.EventLevelName).FontSize(10);
                table.Cell().Background(bgColor).Padding(5).AlignRight()
                    .Text(item.MaleCount.ToString()).FontSize(10);
                table.Cell().Background(bgColor).Padding(5).AlignRight()
                    .Text(item.FemaleCount.ToString()).FontSize(10);
                table.Cell().Background(bgColor).Padding(5).AlignRight()
                    .Text(item.TotalCount.ToString()).FontSize(10);

                rowIndex++;
            }
        });
    }

    private void ComposeFooter(IContainer container)
    {
        container.Row(row =>
        {
            row.RelativeItem().AlignLeft().Text(text =>
            {
                text.Span("Generated: ").FontSize(8);
                text.Span(DateTime.Now.ToString("yyyy-MM-dd HH:mm")).FontSize(8);
            });

            row.RelativeItem().AlignRight().Text(text =>
            {
                text.Span("Page ").FontSize(8);
                text.CurrentPageNumber().FontSize(8);
                text.Span(" of ").FontSize(8);
                text.TotalPages().FontSize(8);
            });
        });
    }
}
