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

export interface IUpdateQuote_Request_DTO {
  status: string;
  date: string;
  reference: 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: IUpdateQuoteItem_Request_DTO[];

  dealer_id: string;
}

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

export function ensureUpdateQuote_Request_DTOIsValid(quoteDTO: IUpdateQuote_Request_DTO) {
  const schema = Joi.object({
    status: Joi.string(),
    date: Joi.string(),
    reference: Joi.string(),
    customer_reference: Joi.string(),
    customer_information: Joi.string(),
    lang_code: Joi.string(),
    currency_code: Joi.string(),
    payment_method: Joi.string().optional().allow(null).allow("").default(""),
    notes: Joi.string().optional().allow(null).allow("").default(""),
    validity: Joi.string().optional().allow(null).allow("").default(""),

    subtotal_price: Joi.object({
      amount: Joi.number().allow(null),
      scale: Joi.number(),
      currency_code: Joi.string(),
    }).optional(),

    discount: Joi.object({
      amount: Joi.number().allow(null),
      scale: Joi.number(),
    }).optional(),

    tax: Joi.object({
      amount: Joi.number().allow(null),
      scale: Joi.number(),
    }).optional(),

    total_price: Joi.object({
      amount: Joi.number().allow(null),
      scale: Joi.number(),
      currency_code: Joi.string(),
    }).optional(),

    items: Joi.array().items(
      Joi.object({
        article_id: Joi.number().optional().allow(null).allow("").default(""),
        quantity: Joi.object({
          amount: Joi.number().allow(null),
          scale: Joi.number(),
        }).optional(),
        description: Joi.string().optional().allow(null).allow("").default(""),
        unit_price: Joi.object({
          amount: Joi.number().allow(null),
          scale: Joi.number(),
          currency_code: Joi.string(),
        }).optional(),
        subtotal_price: Joi.object({
          amount: Joi.number().allow(null),
          scale: Joi.number(),
          currency_code: Joi.string(),
        }).optional(),
        discount: Joi.object({
          amount: Joi.number().allow(null),
          scale: Joi.number(),
        }).optional(),
        total_price: Joi.object({
          amount: Joi.number().allow(null),
          scale: Joi.number(),
          currency_code: Joi.string(),
        }).optional(),
      }).unknown(true)
    ),
  }).unknown(true);

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

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

  return Result.ok(true);
}
