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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

billing_interval: {
        type: DataTypes.ENUM,
      
      

        values: [

"monthly",


"yearly"

        ],

      },

price: {
        type: DataTypes.DECIMAL,
      
      

      },

max_users: {
        type: DataTypes.INTEGER,
      
      

      },

max_jobs_per_month: {
        type: DataTypes.INTEGER,
      
      

      },

is_active: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

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

  plans.associate = (db) => {


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




























    db.plans.hasMany(db.subscriptions, {
      as: 'subscriptions_plan',
      foreignKey: {
          name: 'planId',
      },
      constraints: false,
    });



//end loop



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




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

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



  return plans;
};


