import {
  ICollection,
  ICreateQuote_QuoteItem_Response_DTO,
  ICreateQuote_Response_DTO,
} from "@shared/contexts";
import { Quote, QuoteItem } from "../../../../../../domain";
import { ISalesContext } from "../../../../../Sales.context";

export interface ICreateQuotePresenter {
  map: (quote: Quote, context: ISalesContext) => ICreateQuote_Response_DTO;
}

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

      payment_method: quote.paymentMethod.toString(),
      validity: quote.validity.toString(),
      notes: quote.notes.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(),

      items: quoteItemPresenter(quote.items, context),
      dealer_id: quote.dealerId.toString(),
    };
  },
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const quoteItemPresenter = (
  items: ICollection<QuoteItem>,
  context: ISalesContext
): ICreateQuote_QuoteItem_Response_DTO[] =>
  items.totalCount > 0
    ? items.items.map((item: QuoteItem) => ({
        id_article: item.idArticle.toString(),
        description: item.description.toString(),
        quantity: item.quantity.convertScale(2).toObject(),
        unit_price: item.unitPrice.convertScale(2).toObject(),
        subtotal_price: item.subtotalPrice.convertScale(2).toObject(),
        discount: item.discount.convertScale(2).toObject(),
        total_price: item.totalPrice.convertScale(2).toObject(),
      }))
    : [];
