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

      territory: {
        type: DataTypes.TEXT,
      },

      status: {
        type: DataTypes.ENUM,

        values: ['proposed', 'active', 'completed'],
      },

      projected_roi_pct: {
        type: DataTypes.DECIMAL,
      },

      start_date: {
        type: DataTypes.DATE,
      },

      approval_status: {
        type: DataTypes.ENUM,

        values: ['pending', 'approved', 'rejected'],
      },

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

  expansion_plans.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.expansion_plans.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

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

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

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

  return expansion_plans;
};
