import joi from '@/validator/joi';

const dimensionSchema = joi.object({
  width: joi.number().allow(0).default(0),
  height: joi.number().allow(0).default(0),
  length: joi.number().allow(0).default(0),
});

const stockSettingSchema = joi.object({
  sellable_stock: joi.number().required(),
});

const modelSchema = joi.object({
  id: joi.number().allow(0),
  sku: joi.string().allow(''),
  name: joi.string().allow(''),
  stock: joi.number().required(),
  price: joi.string().pattern(/^\d+$/).required(),
  tier_index: joi.array().items(joi.number()).required(),
  stock_setting_list: joi.array().items(stockSettingSchema).required(),
});

const tierVariationSchema = joi.object({
  name: joi.string().allow('').required(),
  images: joi.array().items(joi.string().allow('')).default([]),
  options: joi.array().items(joi.string().allow('')).default([]),
});

const logisticsChannelSchema = joi.object({
  price: joi.string().pattern(/^\d+$/).required(),
  cover_shipping_fee: joi.boolean().required(),
  enabled: joi.boolean().required(),
  item_flag: joi.string().required(),
  channelid: joi.number().required(),
  sizeid: joi.number().required(),
});

const attributeSchema = joi.object({
  attribute_name: joi.string(),
  attribute_id: joi.number(),
  mandatory: joi.boolean(),
  attribute_value_id: joi.number(),
  attribute_text: joi.string(),
  custom_value: joi.object({
    raw_value: joi.string().allow(''),
    unit: joi.string().allow(''),
  }),
}).xor('attribute_value_id', 'custom_value');

export const validatorProductCreate = joi.object({
  id: joi.number().required(),
  name: joi.string().required(),
  brand: joi.string().required(),
  brand_id: joi.number().required(),
  images: joi.array().items(joi.string()).required(),
  long_images: joi.array().items(joi.string()).required(),
  description: joi.string().required(),
  category_path: joi.array().items(joi.number()).required(),
  attributes: joi.array().items(attributeSchema).required(),
  category_recommend: joi.array().items(joi.any()).required(),
  price: joi.string().pattern(/^\d+$/).required(),
  parent_sku: joi.string().required(),
  wholesale_list: joi.array().items(joi.any()).required(),
  installment_tenures: joi.object().required(),
  weight: joi.number().min(0).required(),
  dimension: dimensionSchema.required(),
  model_list: joi.array().items(modelSchema).required(),
  tier_variation: joi.array().items(tierVariationSchema).required(),
  logistics_channels: joi.array().items(logisticsChannelSchema).required(),
  unlisted: joi.boolean().required(),
  add_on_deal: joi.array().items(joi.any()).required(),
  video_list: joi.array().optional().default([]),
  days_to_ship: joi.number().required(),
  ds_cat_rcmd_id: joi.string().allow(''),
  condition: joi.number().required(),
  size_chart: joi.any().allow(null),
  size_chart_info: joi.any().allow(null),
  pre_order: joi.boolean().required(),
  installment_tenrues: joi.object().allow({}),
  stock: joi.number().required(),
  stock_setting_list: joi.array().items(stockSettingSchema).required(),
});
