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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

objective: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"planned",


"active",


"completed",


"archived"

        ],

      },

planned_start_at: {
        type: DataTypes.DATE,
      
      

      },

planned_end_at: {
        type: DataTypes.DATE,
      
      

      },

actual_start_at: {
        type: DataTypes.DATE,
      
      

      },

actual_end_at: {
        type: DataTypes.DATE,
      
      

      },

protocol_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  trials.associate = (db) => {

    db.trials.belongsToMany(db.trial_sites, {
      as: 'trial_sites',
      foreignKey: {
        name: 'trials_trial_sitesId',
      },
      constraints: false,
      through: 'trialsTrial_sitesTrial_sites',
    });

    db.trials.belongsToMany(db.trial_sites, {
      as: 'trial_sites_filter',
      foreignKey: {
        name: 'trials_trial_sitesId',
      },
      constraints: false,
      through: 'trialsTrial_sitesTrial_sites',
    });

    db.trials.belongsToMany(db.trial_entries, {
      as: 'trial_entries',
      foreignKey: {
        name: 'trials_trial_entriesId',
      },
      constraints: false,
      through: 'trialsTrial_entriesTrial_entries',
    });

    db.trials.belongsToMany(db.trial_entries, {
      as: 'trial_entries_filter',
      foreignKey: {
        name: 'trials_trial_entriesId',
      },
      constraints: false,
      through: 'trialsTrial_entriesTrial_entries',
    });


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









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



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




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






    db.trials.hasMany(db.reports, {
      as: 'reports_trial',
      foreignKey: {
          name: 'trialId',
      },
      constraints: false,
    });




//end loop



    db.trials.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.trials.belongsTo(db.farms, {
      as: 'farm',
      foreignKey: {
        name: 'farmId',
      },
      constraints: false,
    });

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




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

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



  return trials;
};


