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

observed_at: {
        type: DataTypes.DATE,
      
      

      },

event_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"scouting",


"measurement",


"application",


"harvest",


"issue"

        ],

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  observation_events.associate = (db) => {

    db.observation_events.belongsToMany(db.observations, {
      as: 'observations',
      foreignKey: {
        name: 'observation_events_observationsId',
      },
      constraints: false,
      through: 'observation_eventsObservationsObservations',
    });

    db.observation_events.belongsToMany(db.observations, {
      as: 'observations_filter',
      foreignKey: {
        name: 'observation_events_observationsId',
      },
      constraints: false,
      through: 'observation_eventsObservationsObservations',
    });

    db.observation_events.belongsToMany(db.media_assets, {
      as: 'media_assets',
      foreignKey: {
        name: 'observation_events_media_assetsId',
      },
      constraints: false,
      through: 'observation_eventsMedia_assetsMedia_assets',
    });

    db.observation_events.belongsToMany(db.media_assets, {
      as: 'media_assets_filter',
      foreignKey: {
        name: 'observation_events_media_assetsId',
      },
      constraints: false,
      through: 'observation_eventsMedia_assetsMedia_assets',
    });


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
















    db.observation_events.hasMany(db.observations, {
      as: 'observations_observation_event',
      foreignKey: {
          name: 'observation_eventId',
      },
      constraints: false,
    });







//end loop



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

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

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

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

    db.observation_events.belongsTo(db.users, {
      as: 'observer',
      foreignKey: {
        name: 'observerId',
      },
      constraints: false,
    });

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




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

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



  return observation_events;
};


