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

name: {
        type: DataTypes.TEXT,
      
      

      },

greenhouse_block: {
        type: DataTypes.TEXT,
      
      

      },

location_notes: {
        type: DataTypes.TEXT,
      
      

      },

rows_count: {
        type: DataTypes.INTEGER,
      
      

      },

columns_count: {
        type: DataTypes.INTEGER,
      
      

      },

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_sites.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_sites.hasMany(db.plots, {
      as: 'plots_trial_site',
      foreignKey: {
          name: 'trial_siteId',
      },
      constraints: false,
    });



    db.trial_sites.hasMany(db.observation_events, {
      as: 'observation_events_trial_site',
      foreignKey: {
          name: 'trial_siteId',
      },
      constraints: false,
    });









//end loop



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

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




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

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



  return trial_sites;
};


