import { Collection, ensureBoolean, ensureNumber, ensureString } from "@repo/rdx-utils";
import { Contact } from "../../../domain";
import { IListContactsResponseDTO } from "../../dto";

export interface IListContactsPresenter {
  toDTO: (contacts: Collection<Contact>) => IListContactsResponseDTO[];
}

export const listContactsPresenter: IListContactsPresenter = {
  toDTO: (contacts: Collection<Contact>): IListContactsResponseDTO[] =>
    contacts.map((contact) => ({
      id: ensureString(contact.id.toString()),
      reference: ensureString(contact.reference),

      is_companyr: ensureBoolean(contact.isFreelancer),
      name: ensureString(contact.name),
      trade_name: ensureString(contact.tradeName.getOrUndefined()),
      tin: ensureString(contact.tin.toString()),

      street: ensureString(contact.address.street),
      city: ensureString(contact.address.city),
      state: ensureString(contact.address.state),
      postal_code: ensureString(contact.address.postalCode),
      country: ensureString(contact.address.country),

      email: ensureString(contact.email.toString()),
      phone: ensureString(contact.phone.toString()),
      fax: ensureString(contact.fax.getOrUndefined()?.toString()),
      website: ensureString(contact.website.getOrUndefined()),

      legal_record: ensureString(contact.legalRecord),

      default_tax: ensureNumber(contact.defaultTax),
      status: ensureString(contact.isActive ? "active" : "inactive"),
      language_code: ensureString(contact.langCode),
      currency_code: ensureString(contact.currencyCode),
    })),
};
