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

name: {
        type: DataTypes.TEXT,
      
      

      },

plan_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"standard",


"non_refundable",


"package",


"corporate",


"promo"

        ],

      },

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

      },

min_nights: {
        type: DataTypes.INTEGER,
      
      

      },

max_nights: {
        type: DataTypes.INTEGER,
      
      

      },

deposit_percent: {
        type: DataTypes.DECIMAL,
      
      

      },

cancellation_policy: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  rate_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.rate_plans.hasMany(db.seasonal_rates, {
      as: 'seasonal_rates_rate_plan',
      foreignKey: {
          name: 'rate_planId',
      },
      constraints: false,
    });


    db.rate_plans.hasMany(db.dynamic_pricing_rules, {
      as: 'dynamic_pricing_rules_rate_plan',
      foreignKey: {
          name: 'rate_planId',
      },
      constraints: false,
    });




    db.rate_plans.hasMany(db.reservations, {
      as: 'reservations_rate_plan',
      foreignKey: {
          name: 'rate_planId',
      },
      constraints: false,
    });
















//end loop



    db.rate_plans.belongsTo(db.hotels, {
      as: 'hotel',
      foreignKey: {
        name: 'hotelId',
      },
      constraints: false,
    });

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




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

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



  return rate_plans;
};


