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

delivery_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"scheduled",


"in_transit",


"arrived",


"unloaded",


"checked_in",


"returned"

        ],

      },

departed_at: {
        type: DataTypes.DATE,
      
      

      },

arrived_at: {
        type: DataTypes.DATE,
      
      

      },

delivery_note: {
        type: DataTypes.TEXT,
      
      

      },

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

  deliveries.associate = (db) => {


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



















































//end loop



    db.deliveries.belongsTo(db.purchase_orders, {
      as: 'purchase_order',
      foreignKey: {
        name: 'purchase_orderId',
      },
      constraints: false,
    });

    db.deliveries.belongsTo(db.market_stores, {
      as: 'store',
      foreignKey: {
        name: 'storeId',
      },
      constraints: false,
    });

    db.deliveries.belongsTo(db.employees, {
      as: 'received_by_employee',
      foreignKey: {
        name: 'received_by_employeeId',
      },
      constraints: false,
    });



    db.deliveries.hasMany(db.file, {
      as: 'delivery_documents',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.deliveries.getTableName(),
        belongsToColumn: 'delivery_documents',
      },
    });


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

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



  return deliveries;
};


