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

shipment_number: {
        type: DataTypes.TEXT,
      
      

      },

shipped_at: {
        type: DataTypes.DATE,
      
      

      },

shipment_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"planned",


"packed",


"shipped",


"delivered",


"cancelled"

        ],

      },

carrier: {
        type: DataTypes.TEXT,
      
      

      },

tracking_number: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  shipments.associate = (db) => {


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





























    db.shipments.hasMany(db.shipment_lines, {
      as: 'shipment_lines_shipment',
      foreignKey: {
          name: 'shipmentId',
      },
      constraints: false,
    });









//end loop



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

    db.shipments.belongsTo(db.locations, {
      as: 'from_location',
      foreignKey: {
        name: 'from_locationId',
      },
      constraints: false,
    });

    db.shipments.belongsTo(db.users, {
      as: 'shipped_by',
      foreignKey: {
        name: 'shipped_byId',
      },
      constraints: false,
    });



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


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

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



  return shipments;
};


