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

      comp_package: {
        type: DataTypes.TEXT,
      },

      target_start_date: {
        type: DataTypes.DATE,
      },

      status: {
        type: DataTypes.ENUM,

        values: ['Draft', 'Sent', 'Accepted', 'Declined'],
      },

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

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

    //end loop

    db.offers.belongsTo(db.applications, {
      as: 'application',
      foreignKey: {
        name: 'applicationId',
      },
      constraints: false,
    });

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

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

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

  return offers;
};
