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,
      },

order_number: {
        type: DataTypes.TEXT,
      
      

      },

ordered_at: {
        type: DataTypes.DATE,
      
      

      },

order_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"confirmed",


"packed",


"shipped",


"delivered",


"canceled",


"refunded"

        ],

      },

subtotal_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

shipping_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

delivery_address: {
        type: DataTypes.TEXT,
      
      

      },

buyer_notes: {
        type: DataTypes.TEXT,
      
      

      },

expected_delivery_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,
    });


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


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










//end loop



    db.orders.belongsTo(db.tenants, {
      as: 'buyer_tenant',
      foreignKey: {
        name: 'buyer_tenantId',
      },
      constraints: false,
    });

    db.orders.belongsTo(db.tenants, {
      as: 'seller_tenant',
      foreignKey: {
        name: 'seller_tenantId',
      },
      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;
};


