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

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"PLANNED",


"ASSIGNED",


"IN_TRANSIT",


"DELIVERED",


"CANCELLED"

        ],

      },

load_no: {
        type: DataTypes.TEXT,
      
      

      },

reference: {
        type: DataTypes.TEXT,
      
      

      },

equipment: {
        type: DataTypes.ENUM,
      
      

        values: [

"NONE",


"FRIGO",


"ADR",


"LIFTGATE",


"DOUBLE_CREW",


"OTHER"

        ],

      },

cargo_description: {
        type: DataTypes.TEXT,
      
      

      },

weight_kg: {
        type: DataTypes.INTEGER,
      
      

      },

volume_m3: {
        type: DataTypes.DECIMAL,
      
      

      },

pallets: {
        type: DataTypes.INTEGER,
      
      

      },

pieces: {
        type: DataTypes.INTEGER,
      
      

      },

temperature_min_c: {
        type: DataTypes.DECIMAL,
      
      

      },

temperature_max_c: {
        type: DataTypes.DECIMAL,
      
      

      },

adr_required: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

cmr_number: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  loads.associate = (db) => {


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



































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



    db.loads.hasMany(db.trip_loads, {
      as: 'trip_loads_load',
      foreignKey: {
          name: 'loadId',
      },
      constraints: false,
    });




    db.loads.hasMany(db.invoice_lines, {
      as: 'invoice_lines_load',
      foreignKey: {
          name: 'loadId',
      },
      constraints: false,
    });



    db.loads.hasMany(db.cost_items, {
      as: 'cost_items_load',
      foreignKey: {
          name: 'loadId',
      },
      constraints: false,
    });






//end loop



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

    db.loads.belongsTo(db.orders, {
      as: 'order',
      foreignKey: {
        name: 'orderId',
      },
      constraints: false,
    });




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

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



  return loads;
};


