const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');

module.exports = function(sequelize, DataTypes) {
  const orders = sequelize.define(
    'orders',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

shipping_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_total: {
        type: DataTypes.DECIMAL,
      
      

      },

total: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"paid",


"processing",


"shipped",


"delivered",


"canceled",


"refunded"

        ],

      },

payment_method: {
        type: DataTypes.ENUM,
      
      

        values: [

"demo_wallet",


"card_placeholder"

        ],

      },

placed_at: {
        type: DataTypes.DATE,
      
      

      },

paid_at: {
        type: DataTypes.DATE,
      
      

      },

fulfilled_at: {
        type: DataTypes.DATE,
      
      

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  orders.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity




























    db.orders.hasMany(db.order_items, {
      as: 'order_items_order',
      foreignKey: {
          name: 'orderId',
      },
      constraints: false,
    });









//end loop



    db.orders.belongsTo(db.users, {
      as: 'buyer_user',
      foreignKey: {
        name: 'buyer_userId',
      },
      constraints: false,
    });

    db.orders.belongsTo(db.merch_stores, {
      as: 'merch_store',
      foreignKey: {
        name: 'merch_storeId',
      },
      constraints: false,
    });

    db.orders.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




    db.orders.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.orders.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };



  return orders;
};


