import { ICollection, IListQuotes_Response_DTO, IListResponse_DTO } from "@shared/contexts";
import { Quote } from "../../../../../../domain";
import { ISalesContext } from "../../../../../Sales.context";

export interface IListQuotesPresenter {
  map: (quote: Quote, context: ISalesContext) => IListQuotes_Response_DTO;
  mapArray: (
    articles: ICollection<Quote>,
    context: ISalesContext,
    params: {
      page: number;
      limit: number;
    }
  ) => IListResponse_DTO<IListQuotes_Response_DTO>;
}

export const ListQuotesPresenter: IListQuotesPresenter = {
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  map: (quote: Quote, context: ISalesContext): IListQuotes_Response_DTO => {
    return {
      id: quote.id.toString(),
      status: quote.status.toString(),
      date: quote.date.toISO8601(),
      reference: quote.reference.toString(),
      customer_information: quote.customer.toString(),
      customer_reference: quote.customerReference.toString(),
      lang_code: quote.language.toString(),
      currency_code: quote.currency.toString(),

      subtotal_price: quote.subtotalPrice.convertScale(2).toObject(),

      discount: quote.discount.convertScale(2).toObject(),
      discount_price: quote.discountPrice.convertScale(2).toObject(),

      before_tax_price: quote.beforeTaxPrice.convertScale(2).toObject(),

      tax: quote.tax.convertScale(2).toObject(),
      tax_price: quote.taxPrice.convertScale(2).toObject(),

      total_price: quote.totalPrice.convertScale(2).toObject(),
      dealer_id: quote.dealerId.toString(),

      date_sent: quote.dateSent.toISO8601(),
    };
  },

  mapArray: (
    quotes: ICollection<Quote>,
    context: ISalesContext,
    params: {
      page: number;
      limit: number;
    }
  ): IListResponse_DTO<IListQuotes_Response_DTO> => {
    const { page, limit } = params;

    const totalCount = quotes.totalCount ?? 0;
    const items = quotes.items.map((quote: Quote) => ListQuotesPresenter.map(quote, context));

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

    return result;
  },
};
