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

title: {
        type: DataTypes.TEXT,
      
      

      },

deal_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"vendor_finance",


"management_buyout",


"asset_purchase",


"share_purchase",


"joint_venture",


"strategic_partnership",


"licensing",


"distribution"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"in_review",


"ready",


"sent",


"archived"

        ],

      },

target_price: {
        type: DataTypes.DECIMAL,
      
      

      },

down_payment: {
        type: DataTypes.DECIMAL,
      
      

      },

seller_note_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

interest_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

term_months: {
        type: DataTypes.INTEGER,
      
      

      },

structure_overview: {
        type: DataTypes.TEXT,
      
      

      },

risk_factors: {
        type: DataTypes.TEXT,
      
      

      },

next_steps: {
        type: DataTypes.TEXT,
      
      

      },

created_at_time: {
        type: DataTypes.DATE,
      
      

      },

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

  deal_packages.associate = (db) => {

    db.deal_packages.belongsToMany(db.partner_targets, {
      as: 'partner_targets',
      foreignKey: {
        name: 'deal_packages_partner_targetsId',
      },
      constraints: false,
      through: 'deal_packagesPartner_targetsPartner_targets',
    });

    db.deal_packages.belongsToMany(db.partner_targets, {
      as: 'partner_targets_filter',
      foreignKey: {
        name: 'deal_packages_partner_targetsId',
      },
      constraints: false,
      through: 'deal_packagesPartner_targetsPartner_targets',
    });


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


















    db.deal_packages.hasMany(db.partner_targets, {
      as: 'partner_targets_deal_package',
      foreignKey: {
          name: 'deal_packageId',
      },
      constraints: false,
    });




//end loop



    db.deal_packages.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });

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




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

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



  return deal_packages;
};


