import { CriteriaDTO } from "@erp/core";
import { Presenter } from "@erp/core/api";
import { CustomerListDTO } from "@erp/customer-invoices/api/infrastructure";
import { Criteria } from "@repo/rdx-criteria/server";
import { toEmptyString } from "@repo/rdx-ddd";
import { Collection } from "@repo/rdx-utils";
import { ListCustomersResponseDTO } from "../../../../common/dto";

export class ListCustomersPresenter extends Presenter {
  protected _mapCustomer(customer: CustomerListDTO) {
    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()),

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

      metadata: {
        entity: "customer",
        //created_at: customer.createdAt.toPrimitive(),
        //updated_at: customer.updatedAt.toPrimitive()
      },
    };
  }

  toOutput(params: {
    customers: Collection<CustomerListDTO>;
    criteria: Criteria;
  }): ListCustomersResponseDTO {
    const { customers, criteria } = params;

    const items = customers.map((customer) => this._mapCustomer(customer));
    const totalItems = customers.total();

    return {
      page: criteria.pageNumber,
      per_page: criteria.pageSize,
      total_pages: Math.ceil(totalItems / criteria.pageSize),
      total_items: totalItems,
      items: items,
      metadata: {
        entity: "customers",
        criteria: criteria.toJSON() as CriteriaDTO,
        //links: {
        //  self: `/api/customer-invoices?page=${criteria.pageNumber}&per_page=${criteria.pageSize}`,
        //  first: `/api/customer-invoices?page=1&per_page=${criteria.pageSize}`,
        //  last: `/api/customer-invoices?page=${Math.ceil(totalItems / criteria.pageSize)}&per_page=${criteria.pageSize}`,
        //},
      },
    };
  }
}
