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

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"planned",


"picked",


"shipped",


"delivered",


"cancelled"

        ],

      },

planned_ship_at: {
        type: DataTypes.DATE,
      
      

      },

planned_delivery_at: {
        type: DataTypes.DATE,
      
      

      },

delivered_at: {
        type: DataTypes.DATE,
      
      

      },

ship_to_address: {
        type: DataTypes.TEXT,
      
      

      },

carrier: {
        type: DataTypes.TEXT,
      
      

      },

tracking_number: {
        type: DataTypes.TEXT,
      
      

      },

shipping_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

notes: {
        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


























    db.deliveries.hasMany(db.delivery_items, {
      as: 'delivery_items_delivery',
      foreignKey: {
          name: 'deliveryId',
      },
      constraints: false,
    });







//end loop



    db.deliveries.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });



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


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

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



  return deliveries;
};


