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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

price: {
        type: DataTypes.DECIMAL,
      
      

      },

billing_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"one_time",


"subscription"

        ],

      },

stripe_price_ref: {
        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,
    },
  );

  service_packages.associate = (db) => {


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











    db.service_packages.hasMany(db.service_requests, {
      as: 'service_requests_service_package',
      foreignKey: {
          name: 'service_packageId',
      },
      constraints: false,
    });










    db.service_packages.hasMany(db.subscriptions, {
      as: 'subscriptions_service_package',
      foreignKey: {
          name: 'service_packageId',
      },
      constraints: false,
    });













//end loop



    db.service_packages.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });




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

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



  return service_packages;
};


