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

plot_code: {
        type: DataTypes.TEXT,
      
      

      },

row_number: {
        type: DataTypes.INTEGER,
      
      

      },

column_number: {
        type: DataTypes.INTEGER,
      
      

      },

area_sq_m: {
        type: DataTypes.DECIMAL,
      
      

      },

target_plant_count: {
        type: DataTypes.INTEGER,
      
      

      },

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,
    },
  );

  plots.associate = (db) => {


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













    db.plots.hasMany(db.plants, {
      as: 'plants_plot',
      foreignKey: {
          name: 'plotId',
      },
      constraints: false,
    });


    db.plots.hasMany(db.observation_events, {
      as: 'observation_events_plot',
      foreignKey: {
          name: 'plotId',
      },
      constraints: false,
    });









//end loop



    db.plots.belongsTo(db.trial_sites, {
      as: 'trial_site',
      foreignKey: {
        name: 'trial_siteId',
      },
      constraints: false,
    });

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

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




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

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



  return plots;
};


