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_no: {
        type: DataTypes.TEXT,
      
      

      },

reference: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"DRAFT",


"CONFIRMED",


"IN_PROGRESS",


"COMPLETED",


"CANCELLED"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"EUR",


"USD",


"GBP",


"PLN",


"CZK",


"RON",


"BGN",


"OTHER"

        ],

      },

agreed_price: {
        type: DataTypes.DECIMAL,
      
      

      },

payment_terms_days: {
        type: DataTypes.INTEGER,
      
      

      },

planned_start_at: {
        type: DataTypes.DATE,
      
      

      },

planned_end_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.loads, {
      as: 'loads_order',
      foreignKey: {
          name: 'orderId',
      },
      constraints: false,
    });






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









//end loop



    db.orders.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

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




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

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



  return orders;
};


