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

      domain: {
        type: DataTypes.TEXT,
      },

      status: {
        type: DataTypes.ENUM,

        values: ['UNVERIFIED', 'VERIFYING', 'ACTIVE', 'ERROR'],
      },

      verification_token: {
        type: DataTypes.TEXT,
      },

      ssl_status: {
        type: DataTypes.ENUM,

        values: ['PENDING', 'ACTIVE', 'ERROR'],
      },

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

  domain_mappings.associate = (db) => {
    /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

    //end loop

    db.domain_mappings.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

    db.domain_mappings.belongsTo(db.profile_pages, {
      as: 'profile_page',
      foreignKey: {
        name: 'profile_pageId',
      },
      constraints: false,
    });

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

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

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

  return domain_mappings;
};
