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

namespace SportsDivision.Web.Reports;

public class StudentsBySchoolDocument : IDocument
{
    private readonly IEnumerable<StudentsBySchoolReportDto> _data;
    private readonly string _tournamentName;

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

    public StudentsBySchoolDocument(IEnumerable<StudentsBySchoolReportDto> 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).Column(column =>
        {
            foreach (var school in _data)
            {
                // School heading
                column.Item().PaddingTop(10).Background("#E8EEF4").Padding(8).Text(text =>
                {
                    text.Span(school.SchoolName).FontSize(12).Bold().FontColor(HeaderColor);
                    text.Span($"  (Zone: {school.ZoneName})").FontSize(10).FontColor("#666666");
                });

                // Students table
                column.Item().PaddingTop(5).PaddingBottom(5).Table(table =>
                {
                    table.ColumnsDefinition(columns =>
                    {
                        columns.RelativeColumn(2);  // Student Name
                        columns.ConstantColumn(50);  // Sex
                        columns.RelativeColumn(4);  // Events
                    });

                    table.Header(header =>
                    {
                        header.Cell().Background(HeaderColor).Padding(5)
                            .Text("Student Name").FontColor(Colors.White).FontSize(10).Bold();
                        header.Cell().Background(HeaderColor).Padding(5).AlignCenter()
                            .Text("Sex").FontColor(Colors.White).FontSize(10).Bold();
                        header.Cell().Background(HeaderColor).Padding(5)
                            .Text("Events").FontColor(Colors.White).FontSize(10).Bold();
                    });

                    var rowIndex = 0;
                    foreach (var student in school.Students)
                    {
                        var bgColor = rowIndex % 2 == 0 ? Colors.White : AltRowColor;

                        table.Cell().Background(bgColor).Padding(5)
                            .Text(student.StudentName).FontSize(10);
                        table.Cell().Background(bgColor).Padding(5).AlignCenter()
                            .Text(student.Sex).FontSize(10);
                        table.Cell().Background(bgColor).Padding(5)
                            .Text(string.Join(", ", student.Events)).FontSize(9);

                        rowIndex++;
                    }
                });

                column.Item().PaddingVertical(3).LineHorizontal(0.5f).LineColor("#CCCCCC");
            }
        });
    }

    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);
            });
        });
    }
}
