const mongoose = require("mongoose");
const Schema = mongoose.Schema
const {onOrderCreated} = require('../apps/common/Events')

const WmsPoSchema = mongoose.Schema({
  merchantId: {
    type: String
  },
  storeDomain: {
    type: String,
    required: true
  },
  orderId: {
    type: String,
    required: true
  },
  productOrderDate:{type: String},
  product_name: { type: String },
  currency: { type: String },
  orderSerial :  { type: String },
  created_on: {type: Date},
  created_at: { // record created date
    type: Date,
    default: new Date().toISOString()
  },
  products: { type: Array },
  orderNumber: { type: String },
  price: { type: Number },
  // customer: { type: Array },
  customer: {
    email: {type: String},
    first_name: { type: String }
  },
  resellerPrice:{ type: Number },
  varient: { type: Array },
  quantity: { type: Number },
  paid: { type: Number },
  paymentMode: {type:String},
  fulfillmentStatus: { type: String,default: "Unfulfilled" },
  marketPlace: { type: String },
  tracking_number:{type:String, default:null},
  pStatus: {
    type: String,
    default: "PU"
    // PU = Default, PD = Paid, PR = Partial Refund, PP = Partial Payment
  },
  oStatus: {
    type: String,
    default: "OB"
    // OB = Default, OC = By user intervention
  },
  sStatus: {
    type: String,
    default: "SU"
    // SU = Default, SS = Shipped
  },
  dStatus: {
    type: String,
    default: "DN"
    // DN = Default
  },
  rStatus: {
    type: String,
    default: "RR"
    // RR = Default
  },
  chinaProcess: { 
    type: Number
    // 0 = India, 1 = China
  },
  invoicePo: [{
    orderNumber: { type: Number },
    sku: { type: String },
    qtyOrdered: { type: Number },
    poQty: { type: Number },
    netAmount: { type: Number },
    tax: { type: Number },
    hsn: { type: String },
    shippingAmt: { type: Number },
    shippingTax: { type: Number },
    totalAmt: { type: Number },
    purchaseOrderId: {type: Schema.Types.ObjectId}
  }],
  updated_on:{
    type:Date
  },
  paid_date: {type: Date},
  shipped_date: {type: Date},
  isDelete: {
    type: Boolean,
    default: false
  },
  poFlag: {
    type: Boolean,
    default: false
  },
  purchaseOrderId: {
    type: String
  },
  shippingLines: {
    type: Array
  },
  discountCodes: {type: Array},
  paymentDetails: {type: Object},
  taxLines: {type: Array},
  billingAddress: {type: Object},
  shippingAddress: {type: Object},
  otherData : {type: Object},
  note: {type: String},
  paymentMethod: {type: Array},
  poProcess: {type: Number , default: 0},
  poErrorStatus: {type: Number},
  poError: {type: String},
  oss : {
    type:String,
    default:"b2b"
  },
  fulfilmentsync:{ 
    type: Number,
    default: 0,
    /*
      ** 0 means order is not push in unicommerce
      ** 1 - order is on process to push on unicommerce
      ** 2 - order has pushed on unicommerce
      ** 3 - error occurred
      ** 4 order is on process to update tracking number on unicommerce
      ** 5 - updated tracking number on unicommerce
    */
   },
  source:{ 
    type: Number,
    default: 1,
    /*
      ** 4   social commerce app
      ** 1 - DS Shopify
      ** 2 - DS Amazon
      ** 3 - Droppshipping SFC
    */
   },
   uniware_us:{
     type:Number,
     default: 0,
      /*
      ** 0 means order is not push in unicommerce
      ** 1 - order is on process to push on unicommerce
      ** 2 - order has pushed on unicommerce
      ** 3 - error occurred
    */
   }
},  {
  timestamps: true,
});


WmsPoSchema.post(['save','insertMany'],async function (doc){
  try{
      let orders = [];
      if(Array.isArray(doc)){
        orders = doc;
      } else {
        orders = [doc];
      }
      onOrderCreated(orders).then((data)=>{
        console.log("onOrderCreated",data);
      }).catch((err)=>{
        console.log(err.toString(),"asdf ");
      });
  } catch(err){
      console.log(err);
  }
}); 

module.exports = mongoose.model("Wmspo", WmsPoSchema)