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,
      
      

      },

pricing_model: {
        type: DataTypes.ENUM,
      
      

        values: [

"daily",


"hourly",


"weekly",


"monthly"

        ],

      },

base_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

included_km_per_day: {
        type: DataTypes.INTEGER,
      
      

      },

extra_km_fee: {
        type: DataTypes.DECIMAL,
      
      

      },

deposit_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

terms: {
        type: DataTypes.TEXT,
      
      

      },

is_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.rate_rules, {
      as: 'rate_rules_rate_plan',
      foreignKey: {
          name: 'rate_planId',
      },
      constraints: false,
    });





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












//end loop



    db.rate_plans.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      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;
};


