const mongoose = require("mongoose");
const Schema = mongoose.Schema
//const {onOrderCreated} = require('../apps/common/Events')
 
const OrderSchema = mongoose.Schema({
  merchantId: {
    type: String
  },
  supplierId: {
    type: String
  },
  legacyOrderid: {      
    type: Number,
    default: null
  },
  orderSerial :  { type: String },
  storeDomain: {
    type: String,
    // required: true
  },
  mkpOrderId: {
    type: String,
    // required: true
    // mkpOrderId is marketplace order id
  },
  currency: { type: String,default: 'INR' },
  consumerPrice: {
    type: Number,
  },
  
  orderDetails: [ 
    {
      orderItemId: {
        type: String,
      },
      sku: {
        type: String,
        required: true,
      },
      sellerSku: {
        type: String,
        required: true,
      },
      quantity: {
        type: Number,
        required: true,
      },
      quantityShipped: {
        type: Number,
      },
      consumerPrice: {
        type: Number,
      },
      title: {
        type: String,
      },
      chinaProcess: {
        type: Number
        // 0 = India, 1 = China, 3 = Bulk, 4 = China1
      },
      productPrice: {
        type: Number,
      },
      finalPrice: {
        type: Number,
      }
    },
  ],
  shippingPhoneNumber: {
    type: String,
    default: null
  },
  recipientName: {
    type: String,
  },
  recipientEmail: {
    type: String,
  },
  shippingAddress1: {
    type: String,
    default: null
  },
  shippingAddress2: {
    type: String,
    default: null
  },
  shippingAddress3: {
    type: String,
    default: null
  },
  shippingCity: {
    type: String,
  },
  shippingState: {
    type: String,
  },
  shippingPostalCode: {
    type: String,
  },
  shippingCountry: {
    type: String,
  },
  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, DP = Dispute, DD = Refused
  },
  rStatus: {
    type: String,
    default: "RN"
    // RN = Default, RD = Return
  },
  commentStatus: {
    type: String,
    default: "CN"
    // CN = Default
  },
  disputeRole: {
    type: Number,
    default: 0
    // 0 = Action needed, 1 = Awaiting response, 3 = Refused
  },
  chinaProcess: {
    type: Number
    // 0 = India, 1 = China, 3 = Bulk
  },
  trackingId: {
    type: String,
    default: null
  },
  orderDate: {
    type: Date,
    default: Date.now,
  },
  paidDate: {
    type: Date,
    default: null
  },
  shipDate: {
    type: Date,
    default: null
  },
  ticketDate: {
    type: Date,
    default: null
  },
  cancelDate: {
    type: Date,
    default: null
  },
  processingDate: {
    type: Date,
    default: null
  },
  deliveredDate: {
    type: Date,
    default: null
  },
  shippingMethod: {
    type: String,
    default: null
  },
  transactionId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "WalletTransaction",
  },
  order_shipping: [{
    sm_title: { type: String },
    os_tracking_no: { type: String },
    os_date_deliver: { type: Date },
    os_date_add: { type: Date },
    os_status: { type: String },
    os_apply_status: { type: String },
    os_apply_modi_date: { type: Date },
    carrier_name: { type: String },
    shipping_method: { type: String },
    sku: { type: String }
  }],
  order_dispute: [{
    // orders_id: { type: String },
    userId: { type: String },
    disputeHead: { type: String },
    reason: { type: String },
    img_url: { type: Array },
    dispute_date: { type: Date },   
    createdAt: { type: Date, default: Date.now },
    type: { type: String }, 
  }],
 
  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
    */
   },
   api_return_reseaon: {
    type: String,
    default: null
  }, 
  api_return_status: {
    type: Number,
    default: 0
  },
  source:{
    type: Number,
    default: 1,
    /*
      ** 5   social commerce app
      ** 1 - Shopperr DS
      ** 2 - DS Shopify
      ** 3 - DS Amazon
      ** 4 - Droppshipping SFC
    */
	},isDelete: {
    type: Boolean,
    default: false
  },
	createdAt: {
    type: Date,
    default: Date.now,
  },
  updatedAt: {
    type: Date,
    default: Date.now,
  },
  inoffshelfStatus: {
    type: Number,
    default: 0
  },
  remark: {
    type: String,
    default: null
  },
  packingDimension:{
		type: String,
		default: null
	},
  china1:{
		type: Number,
		default: 0
	},
  china2:{
		type: Number,
		default: 0
	},
  china3:{
		type: Number,
		default: 0
	},
  remark:{
		type: String,
		default: null
	}
   },
);
 
 
OrderSchema.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("Orders", OrderSchema)