import Joi from "joi";
import {
  IMoney_Response_DTO,
  IPercentage_Response_DTO,
  IQuantity_Response_DTO,
  Result,
  RuleValidator,
} from "../../../../../common";

export interface ICreateQuote_Request_DTO {
  id: string;
  status: string;
  date: string;
  customer_reference: string;
  customer_information: string;
  lang_code: string;
  currency_code: string;
  payment_method: string;
  notes: string;
  validity: string;

  subtotal_price: IMoney_Response_DTO;
  discount: IPercentage_Response_DTO;
  tax: IPercentage_Response_DTO;
  total_price: IMoney_Response_DTO;

  items: ICreateQuoteItem_Request_DTO[];

  dealer_id: string;
}

export interface ICreateQuoteItem_Request_DTO {
  id_article: string;
  quantity: IQuantity_Response_DTO;
  description: string;
  unit_price: IMoney_Response_DTO;
  price: IMoney_Response_DTO;
  discount: IPercentage_Response_DTO;
  total_price: IMoney_Response_DTO;
}

export function ensureCreateQuote_Request_DTOIsValid(quoteDTO: ICreateQuote_Request_DTO) {
  const schema = Joi.object({
    id: Joi.string(),
    status: Joi.string(),
    date: Joi.string(),
    reference: Joi.string(),
    customer_reference: Joi.string(),
    customer_information: Joi.string(),
  }).unknown(true);

  const result = RuleValidator.validate<ICreateQuote_Request_DTO>(schema, quoteDTO);

  if (result.isFailure) {
    return Result.fail(result.error);
  }

  return Result.ok(true);
}
