import { Dealer } from "@/contexts/sales/domain";
import { ISalesContext } from "@/contexts/sales/infrastructure/Sales.context";
import { ICollection, IListDealers_Response_DTO, IListResponse_DTO } from "@shared/contexts";

export interface IListDealersPresenter {
  map: (dealer: Dealer, context: ISalesContext) => IListDealers_Response_DTO;

  mapArray: (
    dealers: ICollection<Dealer>,
    context: ISalesContext,
    params: {
      page: number;
      limit: number;
    }
  ) => IListResponse_DTO<IListDealers_Response_DTO>;
}

export const listDealersPresenter: IListDealersPresenter = {
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  map: (dealer: Dealer, context: ISalesContext): IListDealers_Response_DTO => {
    return {
      id: dealer.id.toString(),
      name: dealer.name.toString(),
      lang_code: dealer.language.code,
      status: dealer.status.toString(),
    };
  },

  mapArray: (
    dealers: ICollection<Dealer>,
    context: ISalesContext,
    params: {
      page: number;
      limit: number;
    }
  ): IListResponse_DTO<IListDealers_Response_DTO> => {
    const { page, limit } = params;

    const totalCount = dealers.totalCount ?? 0;
    const items = dealers.items.map((dealer: Dealer) => listDealersPresenter.map(dealer, context));

    const result = {
      page,
      per_page: limit,
      total_pages: Math.ceil(totalCount / limit),
      total_items: totalCount,
      items,
    };

    return result;
  },
};
