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

entry_label: {
        type: DataTypes.TEXT,
      
      

      },

entry_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"test",


"control",


"standard_check"

        ],

      },

treatment: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  trial_entries.associate = (db) => {


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












    db.trial_entries.hasMany(db.plots, {
      as: 'plots_trial_entry',
      foreignKey: {
          name: 'trial_entryId',
      },
      constraints: false,
    });











//end loop



    db.trial_entries.belongsTo(db.trials, {
      as: 'trial',
      foreignKey: {
        name: 'trialId',
      },
      constraints: false,
    });

    db.trial_entries.belongsTo(db.varieties, {
      as: 'variety',
      foreignKey: {
        name: 'varietyId',
      },
      constraints: false,
    });

    db.trial_entries.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return trial_entries;
};


