const mongoose = require('mongoose');
const jwt = require("jsonwebtoken");

const { mongooseConstantValues } = require('../apps/common/systemConstents')
const userScheme = new mongoose.Schema({
	supplier_id: {
		type: String,
		require: true
	},
	merchantId: { type: String, },
	fName: { type: String, },
	balance: { type: Number, default: 0 },
    lName: { type: String, },
    phoneNo: {    
      type: Number,
      unique: true,
    },
	email: {
		type: String,      
		require: true
	},
	password: {
		type: String,
		require: true
	},     
	category: {
		type: String,
		default: "merchant"
	},
	createdAt:{
      type:Date,
	  default: Date.now
	},
	name: {
		type: String,
	},
	location: {
		type: String
	},
	businessName: {
		type: String
	},
	gstNumber: {
		type: String,
	  },
	pInfo: {
		type: Array
	},
	created_on: {
		type: String
	},
	
	origin: {
		type: String
	},
	price_algo: {
		type: String
	},
	is_index: {
		type: Number,
		default: 0
	},
	revenue: {
		type: Number,
		default: 0
	},
	orders: {
		type: Number,
		default: 0
	},
	products: {
		type: Number,
		default: 0
	},
	
	BrandAuthorization: {
		type: String
	},
	BrandLogo: {
		type: String
	},
	TRADEMARKCERTIFICATE: {
		type: String

	},
	ImageOfTheSizeChart: {
		type: String

	},
	ImagePackagingMaterial: {
		type: String

	},
	userType: { type: String, enum: ["Potential", "Active"] },
	remark: { type: String, default: "" },
	pickupADDRESS: {
		type: String

	},
	pickupPinCode: {
		type: String

	},
	pincode: { type: String },
	country: { type: String },
	state: { type: String },
	city: { type: String },
	pickupCITY: {
		type: String

	},
	bankAccountNumber: {
		type: String

	},
	bankIfscCode: {
		type: String
	},

	bankAddress: {
		type: String

	},
	bankAccountHolderName: {
		type: String
	}
	,
	BusinessName: {
		type: String
	},
	BusinessDescription: {
		type: String
	},
	tan: {
		type: String

	},
	GstinNumber: {
		type: String
	},
	amazonStoreData: { type: Array, },
    company: { type: String },
    [mongooseConstantValues.IS_SP_DETAIL_UPDATED_ON_DROPPSHIPPER]: {

      type: Number,// 0 - not updated , 1 - updated
    },
	registerBusinessAddress: {
		type: String
	},
	address: { type: String },
	pan: {
		type: String
	},
	storeData: [
		{
		  storeDomain: {
			type: String,
		  },
		  shopifyApiKey: {
			type: String,
		  },
		  shopifySecretKey: {
			type: String,
		  },
		},
	  ],
	ownAccount: {    
		type: Number,
		default: 0,
	},
	customer_logid:{
		type: String,
		default:null
	},
  loginHistory: [
    {
      date: { type: Date, default: Date.now },
      ip: { type: String },
    },
  ],
  totalLoginCount: { type: Number, default: 0 },
  weeklyLoginCount: { type: Number, default: 0 },
  lastLoginDate: { type: Date },
  lastLoginIP: { type: String },
	updatedAt: { type: Date, default: new Date().toISOString() },
});

userScheme.statics.findByToken = function (token) {
	const User = this;
	let userData;
	try {
		userData = jwt.verify(token, process.env.TOKEN_SECRET);
	} catch (error) {
		console.log(error);
		return Promise.reject(error);
	}
	return User.findOne({
	  _id: userData.id,
	});
};
 
module.exports = mongoose.model('User', userScheme)