import { Presenter } from "@erp/core/api";
import { GetCustomerInvoiceByIdResponseDTO } from "../../../../common/dto";
import {
  FormatMoneyOptions,
  formatDateDTO,
  formatMoneyDTO,
  formatPercentageDTO,
} from "../../helpers";
import { formatPaymentMethodDTO } from "../../helpers/format-payment_method-dto";

export class CustomerInvoiceReportPresenter extends Presenter<
  GetCustomerInvoiceByIdResponseDTO,
  unknown
> {
  toOutput(invoiceDTO: GetCustomerInvoiceByIdResponseDTO) {
    const itemsPresenter = this.presenterRegistry.getPresenter({
      resource: "customer-invoice-items",
      projection: "REPORT",
      format: "JSON",
    });

    const locale = invoiceDTO.language_code;
    const itemsDTO = itemsPresenter.toOutput(invoiceDTO.items, {
      locale,
    });

    const moneyOptions: FormatMoneyOptions = {
      locale,
      hideZeros: true,
      newScale: 2,
    };

    return {
      ...invoiceDTO,
      items: itemsDTO,

      invoice_date: formatDateDTO(invoiceDTO.invoice_date),
      subtotal_amount: formatMoneyDTO(invoiceDTO.subtotal_amount, moneyOptions),
      discount_percentage: formatPercentageDTO(invoiceDTO.discount_percentage, locale),
      discount_amount: formatMoneyDTO(invoiceDTO.discount_amount, moneyOptions),
      taxable_amount: formatMoneyDTO(invoiceDTO.taxable_amount, moneyOptions),
      taxes_amount: formatMoneyDTO(invoiceDTO.taxes_amount, moneyOptions),
      total_amount: formatMoneyDTO(invoiceDTO.total_amount, moneyOptions),

      payment_method: formatPaymentMethodDTO(invoiceDTO.payment_method),
    };
  }
}
