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

slug: {
        type: DataTypes.TEXT,
      
      

      },

page_title: {
        type: DataTypes.TEXT,
      
      

      },

custom_text: {
        type: DataTypes.TEXT,
      
      

      },

theme_color: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

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

  smart_links.associate = (db) => {


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



















    db.smart_links.hasMany(db.smart_link_destinations, {
      as: 'smart_link_destinations_smart_link',
      foreignKey: {
          name: 'smart_linkId',
      },
      constraints: false,
    });


    db.smart_links.hasMany(db.smart_link_clicks, {
      as: 'smart_link_clicks_smart_link',
      foreignKey: {
          name: 'smart_linkId',
      },
      constraints: false,
    });

























//end loop



    db.smart_links.belongsTo(db.releases, {
      as: 'release',
      foreignKey: {
        name: 'releaseId',
      },
      constraints: false,
    });

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



    db.smart_links.hasMany(db.file, {
      as: 'background_image',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.smart_links.getTableName(),
        belongsToColumn: 'background_image',
      },
    });

    db.smart_links.hasMany(db.file, {
      as: 'artist_photo',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.smart_links.getTableName(),
        belongsToColumn: 'artist_photo',
      },
    });


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

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



  return smart_links;
};


