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 sales_orders = sequelize.define(
    'sales_orders',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

order_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"confirmed",


"fulfilled",


"cancelled"

        ],

      },

order_date: {
        type: DataTypes.DATE,
      
      

      },

requested_ship_date: {
        type: DataTypes.DATE,
      
      

      },

promised_ship_date: {
        type: DataTypes.DATE,
      
      

      },

subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_total: {
        type: DataTypes.DECIMAL,
      
      

      },

total: {
        type: DataTypes.DECIMAL,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  sales_orders.associate = (db) => {

    db.sales_orders.belongsToMany(db.sales_order_lines, {
      as: 'sales_order_lines',
      foreignKey: {
        name: 'sales_orders_sales_order_linesId',
      },
      constraints: false,
      through: 'sales_ordersSales_order_linesSales_order_lines',
    });

    db.sales_orders.belongsToMany(db.sales_order_lines, {
      as: 'sales_order_lines_filter',
      foreignKey: {
        name: 'sales_orders_sales_order_linesId',
      },
      constraints: false,
      through: 'sales_ordersSales_order_linesSales_order_lines',
    });


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






















    db.sales_orders.hasMany(db.sales_order_lines, {
      as: 'sales_order_lines_sales_order',
      foreignKey: {
          name: 'sales_orderId',
      },
      constraints: false,
    });






    db.sales_orders.hasMany(db.shipments, {
      as: 'shipments_sales_order',
      foreignKey: {
          name: 'sales_orderId',
      },
      constraints: false,
    });



    db.sales_orders.hasMany(db.invoices, {
      as: 'invoices_sales_order',
      foreignKey: {
          name: 'sales_orderId',
      },
      constraints: false,
    });





//end loop



    db.sales_orders.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.sales_orders.belongsTo(db.customers, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      constraints: false,
    });

    db.sales_orders.belongsTo(db.warehouses, {
      as: 'warehouse',
      foreignKey: {
        name: 'warehouseId',
      },
      constraints: false,
    });

    db.sales_orders.belongsTo(db.price_lists, {
      as: 'price_list',
      foreignKey: {
        name: 'price_listId',
      },
      constraints: false,
    });




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

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



  return sales_orders;
};


