using Books.Api.Domain.Invoices;

namespace Books.Api.Invoicing.Pdf;

/// <summary>
/// Data transfer object containing all information needed to render an invoice or credit note PDF.
/// </summary>
public record InvoicePdfData
{
    // Document type (Invoice or CreditNote)
    public InvoiceType DocumentType { get; init; } = InvoiceType.Invoice;

    /// <summary>
    /// For credit notes: Reference to the original invoice number.
    /// </summary>
    public string? OriginalInvoiceNumber { get; init; }

    /// <summary>
    /// For credit notes: Reason for issuing the credit.
    /// </summary>
    public string? CreditReason { get; init; }

    // Company information
    public required string CompanyName { get; init; }
    public string? CompanyCvr { get; init; }
    public string? CompanyAddress { get; init; }
    public string? CompanyPostalCode { get; init; }
    public string? CompanyCity { get; init; }
    public string CompanyCountry { get; init; } = "DK";

    // Company bank details for payment information
    public string? CompanyBankName { get; init; }
    public string? CompanyBankRegNo { get; init; }      // Danish reg.nr (4 digits)
    public string? CompanyBankAccountNo { get; init; }  // Danish account number
    public string? CompanyIban { get; init; }           // International bank account
    public string? CompanyBic { get; init; }            // BIC/SWIFT code

    // Customer information
    public required string CustomerName { get; init; }
    public string? CustomerCvr { get; init; }
    public string? CustomerAddress { get; init; }
    public string? CustomerPostalCode { get; init; }
    public string? CustomerCity { get; init; }
    public string CustomerCountry { get; init; } = "DK";
    public string? CustomerNumber { get; init; }

    // Invoice/Credit note details
    public required string InvoiceNumber { get; init; }
    public required DateOnly InvoiceDate { get; init; }
    public required DateOnly DueDate { get; init; }
    public int PaymentTermsDays { get; init; }
    public string Currency { get; init; } = "DKK";
    public string? Reference { get; init; }
    public string? Notes { get; init; }

    // Line items
    public required IReadOnlyList<InvoicePdfLineItem> Lines { get; init; }

    // Calculated totals
    public decimal AmountExVat { get; init; }
    public decimal AmountVat { get; init; }
    public decimal AmountTotal { get; init; }

    // VAT summary (grouped by rate)
    public required IReadOnlyList<VatSummaryLine> VatSummary { get; init; }

    /// <summary>
    /// Returns true if this is a credit note.
    /// </summary>
    public bool IsCreditNote => DocumentType == InvoiceType.CreditNote;
}

/// <summary>
/// Line item for the invoice PDF.
/// </summary>
public record InvoicePdfLineItem
{
    public int LineNumber { get; init; }
    public required string Description { get; init; }
    public decimal Quantity { get; init; }
    public string? Unit { get; init; }
    public decimal UnitPrice { get; init; }
    public decimal DiscountPercent { get; init; }
    public string VatCode { get; init; } = "U25";
    public decimal AmountExVat { get; init; }
    public decimal AmountVat { get; init; }
    public decimal AmountTotal { get; init; }
}

/// <summary>
/// VAT summary line for the totals section.
/// </summary>
public record VatSummaryLine
{
    public string VatCode { get; init; } = "U25";
    public decimal VatRate { get; init; }
    public decimal BasisAmount { get; init; }
    public decimal VatAmount { get; init; }
}
