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

type: {
        type: DataTypes.ENUM,
      
      

        values: [

"PICKUP",


"DELIVERY",


"WAYPOINT",


"DEPOT",


"BORDER",


"OTHER"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"PLANNED",


"ARRIVED",


"LOADED",


"DEPARTED",


"DELIVERED",


"SKIPPED"

        ],

      },

sequence: {
        type: DataTypes.INTEGER,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

address_line1: {
        type: DataTypes.TEXT,
      
      

      },

address_line2: {
        type: DataTypes.TEXT,
      
      

      },

postal_code: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

state: {
        type: DataTypes.TEXT,
      
      

      },

country_code: {
        type: DataTypes.TEXT,
      
      

      },

window_start_at: {
        type: DataTypes.DATE,
      
      

      },

window_end_at: {
        type: DataTypes.DATE,
      
      

      },

planned_at: {
        type: DataTypes.DATE,
      
      

      },

arrived_at: {
        type: DataTypes.DATE,
      
      

      },

departed_at: {
        type: DataTypes.DATE,
      
      

      },

contact_name: {
        type: DataTypes.TEXT,
      
      

      },

contact_phone: {
        type: DataTypes.TEXT,
      
      

      },

contact_email: {
        type: DataTypes.TEXT,
      
      

      },

instructions: {
        type: DataTypes.TEXT,
      
      

      },

reference_no: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  load_stops.associate = (db) => {


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






































    db.load_stops.hasMany(db.pod_files, {
      as: 'pod_files_stop',
      foreignKey: {
          name: 'stopId',
      },
      constraints: false,
    });










//end loop



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

    db.load_stops.belongsTo(db.loads, {
      as: 'load',
      foreignKey: {
        name: 'loadId',
      },
      constraints: false,
    });




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

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



  return load_stops;
};


