using GestionIntegral.Api.Dtos.Reportes.ViewModels;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
using System.Globalization;
using System.Linq;

namespace GestionIntegral.Api.Controllers.Reportes.PdfTemplates
{
  public class ListadoDistCanMensualDiariosDocument : IDocument
  {
    public ListadoDistCanMensualDiariosViewModel Model { get; }
    private static readonly CultureInfo CultureAr = new CultureInfo("es-AR");

    public ListadoDistCanMensualDiariosDocument(ListadoDistCanMensualDiariosViewModel model)
    {
      Model = model;
    }

    public DocumentMetadata GetMetadata() => DocumentMetadata.Default;

    public void Compose(IDocumentContainer container)
    {
      container.Page(page =>
      {
        page.Margin(1, Unit.Centimetre);
        page.DefaultTextStyle(x => x.FontFamily("Arial").FontSize(9));

        page.Header().Element(ComposeHeader);
        page.Content().Element(ComposeContent);
        page.Footer().AlignCenter().Text(x => { x.Span("Página "); x.CurrentPageNumber(); });
      });
    }

    void ComposeHeader(IContainer container)
    {
      container.Column(column =>
      {
        column.Item().AlignCenter().Text($"Ventas por {Model.TipoDestinatario} desde el {Model.FechaDesde} hasta el {Model.FechaHasta}").SemiBold().FontSize(12);
      });
    }

    void ComposeContent(IContainer container)
    {
      // APLICAMOS LA SOLUCIÓN AQUÍ: .AlignTop()
      container.PaddingTop(1, Unit.Centimetre).AlignTop().Table(table =>
      {
        table.ColumnsDefinition(columns =>
      {
            columns.RelativeColumn(3);   // Nombre
            columns.ConstantColumn(50);  // El Día
            columns.ConstantColumn(50);  // El Plata
            columns.ConstantColumn(60);  // Vendidos
            columns.RelativeColumn(1.5f);  // Imp. El Día
            columns.RelativeColumn(1.5f);  // Imp. El Plata
            columns.RelativeColumn(1.5f);  // Importe Total
          });

        table.Header(header =>
      {
            header.Cell().BorderBottom(1).BorderColor(Colors.Grey.Medium).Padding(4).Text("Nombre").SemiBold();
            header.Cell().BorderBottom(1).BorderColor(Colors.Grey.Medium).Padding(4).AlignCenter().Text("El Día").SemiBold();
            header.Cell().BorderBottom(1).BorderColor(Colors.Grey.Medium).Padding(4).AlignCenter().Text("El Plata").SemiBold();
            header.Cell().BorderBottom(1).BorderColor(Colors.Grey.Medium).Padding(4).AlignCenter().Text("Vendidos").SemiBold();
            header.Cell().BorderBottom(1).BorderColor(Colors.Grey.Medium).Padding(4).AlignRight().Text("Imp. El Día").SemiBold();
            header.Cell().BorderBottom(1).BorderColor(Colors.Grey.Medium).Padding(4).AlignRight().Text("Imp. El Plata").SemiBold();
            header.Cell().BorderBottom(1).BorderColor(Colors.Grey.Medium).Padding(4).AlignRight().Text("Importe Total").SemiBold();
          });

        foreach (var item in Model.Detalles.OrderBy(d => d.Canilla))
        {
          table.Cell().Padding(3).PaddingRight(10).Text(item.Canilla);
          table.Cell().Padding(3).AlignCenter().Text(item.ElDia?.ToString("N0") ?? "0");
          table.Cell().Padding(3).AlignCenter().Text(item.ElPlata?.ToString("N0") ?? "0");
          table.Cell().Padding(3).AlignCenter().Text(item.Vendidos?.ToString("N0") ?? "0");
          table.Cell().Padding(3).AlignRight().Text(item.ImporteElDia?.ToString("C", CultureAr) ?? "$ 0.00");
          table.Cell().Padding(3).AlignRight().Text(item.ImporteElPlata?.ToString("C", CultureAr) ?? "$ 0.00");
          table.Cell().Padding(3).AlignRight().Text(item.ImporteTotal?.ToString("C", CultureAr) ?? "$ 0.00");
        }

        // Fila de Totales
        var totalElDia = Model.Detalles.Sum(x => x.ElDia ?? 0);
        var totalElPlata = Model.Detalles.Sum(x => x.ElPlata ?? 0);
        var totalVendidos = Model.Detalles.Sum(x => x.Vendidos ?? 0);
        var totalImpElDia = Model.Detalles.Sum(x => x.ImporteElDia ?? 0);
        var totalImpElPlata = Model.Detalles.Sum(x => x.ImporteElPlata ?? 0);
        var totalImpTotal = Model.Detalles.Sum(x => x.ImporteTotal ?? 0);

        table.Cell().BorderTop(1.5f).BorderColor(Colors.Black).PaddingTop(4).Text("Totales").SemiBold();
        table.Cell().BorderTop(1.5f).BorderColor(Colors.Black).PaddingTop(4).AlignCenter().Text(t => t.Span(totalElDia.ToString("N0")).SemiBold());
        table.Cell().BorderTop(1.5f).BorderColor(Colors.Black).PaddingTop(4).AlignCenter().Text(t => t.Span(totalElPlata.ToString("N0")).SemiBold());
        table.Cell().BorderTop(1.5f).BorderColor(Colors.Black).PaddingTop(4).AlignCenter().Text(t => t.Span(totalVendidos.ToString("N0")).SemiBold());
        table.Cell().BorderTop(1.5f).BorderColor(Colors.Black).PaddingTop(4).AlignRight().Text(t => t.Span(totalImpElDia.ToString("C", CultureAr)).SemiBold());
        table.Cell().BorderTop(1.5f).BorderColor(Colors.Black).PaddingTop(4).AlignRight().Text(t => t.Span(totalImpElPlata.ToString("C", CultureAr)).SemiBold());
        table.Cell().BorderTop(1.5f).BorderColor(Colors.Black).PaddingTop(4).AlignRight().Text(t => t.Span(totalImpTotal.ToString("C", CultureAr)).SemiBold());
      });
    }
  }
}