import { Presenter } from "@erp/core/api";
import { toEmptyString } from "@repo/rdx-ddd";
import { GetCustomerByIdResponseDTO } from "../../../../common/dto";
import { Customer } from "../../../domain";

export class CustomerFullPresenter extends Presenter<Customer, GetCustomerByIdResponseDTO> {
  toOutput(customer: Customer): GetCustomerByIdResponseDTO {
    const address = customer.address.toPrimitive();

    return {
      id: customer.id.toPrimitive(),
      company_id: customer.companyId.toPrimitive(),

      reference: toEmptyString(customer.reference, (value) => value.toPrimitive()),

      is_company: String(customer.isCompany),
      name: customer.name.toPrimitive(),

      trade_name: toEmptyString(customer.tradeName, (value) => value.toPrimitive()),

      tin: toEmptyString(customer.tin, (value) => value.toPrimitive()),

      street: toEmptyString(address.street, (value) => value.toPrimitive()),
      street2: toEmptyString(address.street2, (value) => value.toPrimitive()),
      city: toEmptyString(address.city, (value) => value.toPrimitive()),
      province: toEmptyString(address.province, (value) => value.toPrimitive()),
      postal_code: toEmptyString(address.postalCode, (value) => value.toPrimitive()),
      country: toEmptyString(address.country, (value) => value.toPrimitive()),

      email_primary: toEmptyString(customer.emailPrimary, (value) => value.toPrimitive()),
      email_secondary: toEmptyString(customer.emailSecondary, (value) => value.toPrimitive()),
      phone_primary: toEmptyString(customer.phonePrimary, (value) => value.toPrimitive()),
      phone_secondary: toEmptyString(customer.phoneSecondary, (value) => value.toPrimitive()),
      mobile_primary: toEmptyString(customer.mobilePrimary, (value) => value.toPrimitive()),
      mobile_secondary: toEmptyString(customer.mobileSecondary, (value) => value.toPrimitive()),

      fax: toEmptyString(customer.fax, (value) => value.toPrimitive()),
      website: toEmptyString(customer.website, (value) => value.toPrimitive()),

      legal_record: toEmptyString(customer.legalRecord, (value) => value.toPrimitive()),

      default_taxes: customer.defaultTaxes.getAll().map((tax) => tax.toString()),

      status: customer.isActive ? "active" : "inactive",
      language_code: customer.languageCode.toPrimitive(),
      currency_code: customer.currencyCode.toPrimitive(),

      metadata: {
        entity: "customer",
      },
    };
  }
}
