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

      name: {
        type: DataTypes.TEXT,
      },

      cycle_start: {
        type: DataTypes.DATE,
      },

      cycle_end: {
        type: DataTypes.DATE,
      },

      status: {
        type: DataTypes.ENUM,

        values: ['Open', 'Closed'],
      },

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

  review_cycles.associate = (db) => {
    /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

    db.review_cycles.hasMany(db.reviews, {
      as: 'reviews_review_cycle',
      foreignKey: {
        name: 'review_cycleId',
      },
      constraints: false,
    });

    //end loop

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

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

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

  return review_cycles;
};
