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

key: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

placement_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"top_strip",


"hero",


"between_sections",


"sidebar",


"popup",


"adsense",


"footer_banner",


"custom"

        ],

      },

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

      },

sort_order: {
        type: DataTypes.INTEGER,
      
      

      },

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

  ad_placements.associate = (db) => {

    db.ad_placements.belongsToMany(db.ads, {
      as: 'ads',
      foreignKey: {
        name: 'ad_placements_adsId',
      },
      constraints: false,
      through: 'ad_placementsAdsAds',
    });

    db.ad_placements.belongsToMany(db.ads, {
      as: 'ads_filter',
      foreignKey: {
        name: 'ad_placements_adsId',
      },
      constraints: false,
      through: 'ad_placementsAdsAds',
    });


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































    db.ad_placements.hasMany(db.ads, {
      as: 'ads_placement',
      foreignKey: {
          name: 'placementId',
      },
      constraints: false,
    });














//end loop






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

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



  return ad_placements;
};


