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

license_code: {
        type: DataTypes.TEXT,
      
      

      },

license_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"software_lock",


"network_dongle"

        ],

      },

issued_at: {
        type: DataTypes.DATE,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"revoked",


"expired"

        ],

      },

hardware_fingerprint: {
        type: DataTypes.TEXT,
      
      

      },

server_fingerprint: {
        type: DataTypes.TEXT,
      
      

      },

db_fingerprint: {
        type: DataTypes.TEXT,
      
      

      },

allowed_clients: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

last_error_message: {
        type: DataTypes.TEXT,
      
      

      },

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

  license_keys.associate = (db) => {


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



















    db.license_keys.hasMany(db.license_activations, {
      as: 'license_activations_license_key',
      foreignKey: {
          name: 'license_keyId',
      },
      constraints: false,
    });































//end loop



    db.license_keys.belongsTo(db.license_customers, {
      as: 'license_customer',
      foreignKey: {
        name: 'license_customerId',
      },
      constraints: false,
    });

    db.license_keys.belongsTo(db.license_products, {
      as: 'product',
      foreignKey: {
        name: 'productId',
      },
      constraints: false,
    });




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

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



  return license_keys;
};


