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

plant_tag: {
        type: DataTypes.TEXT,
      
      

      },

planted_at: {
        type: DataTypes.DATE,
      
      

      },

plant_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"planned",


"alive",


"stressed",


"dead",


"removed"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  plants.associate = (db) => {


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














    db.plants.hasMany(db.observation_events, {
      as: 'observation_events_plant',
      foreignKey: {
          name: 'plantId',
      },
      constraints: false,
    });









//end loop



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

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




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

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



  return plants;
};


