import { MoneyDTOHelper, PercentageDTOHelper, QuantityDTOHelper } from "@erp/core";
import {
  GetCustomerInvoiceByIdResponseDTO,
  UpdateCustomerInvoiceByIdRequestDTO,
} from "../../common";
import { InvoiceContextValue } from "../context";
import { InvoiceFormData } from "./invoice.form.schema";

/**
 * Convierte el DTO completo de API a datos numéricos para el formulario.
 */
export const invoiceDtoToFormAdapter = {
  fromDto(dto: GetCustomerInvoiceByIdResponseDTO, context: InvoiceContextValue): InvoiceFormData {
    const { taxCatalog } = context;
    return {
      invoice_number: dto.invoice_number,
      series: dto.series,

      invoice_date: dto.invoice_date,
      operation_date: dto.operation_date,

      customer_id: dto.customer_id,
      recipient: dto.recipient,

      reference: dto.reference ?? "",
      description: dto.description ?? "",
      notes: dto.notes ?? "",

      language_code: dto.language_code,
      currency_code: dto.currency_code,

      subtotal_amount: MoneyDTOHelper.toNumber(dto.subtotal_amount),
      discount_percentage: PercentageDTOHelper.toNumber(dto.discount_percentage),
      discount_amount: MoneyDTOHelper.toNumber(dto.discount_amount),
      taxable_amount: MoneyDTOHelper.toNumber(dto.taxable_amount),
      taxes_amount: MoneyDTOHelper.toNumber(dto.taxes_amount),
      total_amount: MoneyDTOHelper.toNumber(dto.total_amount),

      taxes: dto.taxes.map((taxItem) => ({
        tax_code: taxItem.tax_code,
        tax_label: taxCatalog.findByCode(taxItem.tax_code).match(
          (tax) => tax.name,
          () => ""
        ),
        taxable_amount: MoneyDTOHelper.toNumber(taxItem.taxable_amount),
        taxes_amount: MoneyDTOHelper.toNumber(taxItem.taxes_amount),
      })),

      items: dto.items.map((item) => ({
        is_non_valued: item.is_non_valued === "true",
        description: item.description ?? "",
        quantity: QuantityDTOHelper.toNumericString(item.quantity),
        unit_amount: MoneyDTOHelper.toNumericString(item.unit_amount),
        subtotal_amount: MoneyDTOHelper.toNumber(item.subtotal_amount),
        discount_percentage: PercentageDTOHelper.toNumericString(item.discount_percentage),
        discount_amount: MoneyDTOHelper.toNumber(item.discount_amount),
        taxable_amount: MoneyDTOHelper.toNumber(item.taxable_amount),
        tax_codes: item.tax_codes ?? [],
        taxes_amount: MoneyDTOHelper.toNumber(item.taxes_amount),
        total_amount: MoneyDTOHelper.toNumber(item.total_amount),
      })),
    };
  },

  toDto(form: InvoiceFormData, context: InvoiceContextValue): UpdateCustomerInvoiceByIdRequestDTO {
    const { currency_code } = context;
    return {
      series: form.series,

      invoice_date: form.invoice_date,
      operation_date: form.operation_date,

      customer_id: form.customer_id,

      reference: form.reference,
      description: form.description,
      notes: form.notes,

      language_code: context.language_code,
      currency_code: context.currency_code,

      items: form.items?.map((item) => ({
        is_non_valued: item.is_non_valued ? "true" : "false",
        description: item.description,
        quantity: QuantityDTOHelper.fromNumericString(item.quantity, 4),
        unit_amount: MoneyDTOHelper.fromNumericString(item.unit_amount, currency_code, 4),
        discount_percentage: PercentageDTOHelper.fromNumericString(item.discount_percentage, 2),
        tax_codes: item.tax_codes,
      })),
    };
  },
};
