import { Account } from "@/contexts/accounts/domain";
import { Collection, ensureBoolean, ensureNumber, ensureString } from "@repo/rdx-utils";
import { IListAccountsResponseDTO } from "../../dto";

export interface IListAccountsPresenter {
  toDTO: (accounts: Collection<Account>) => IListAccountsResponseDTO[];
}

export const listAccountsPresenter: IListAccountsPresenter = {
  toDTO: (accounts: Collection<Account>): IListAccountsResponseDTO[] =>
    accounts.map((account) => ({
      id: ensureString(account.id.toString()),

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

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

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

      legal_record: ensureString(account.legalRecord),

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