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

ad_name: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

ad_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"image",


"custom_code"

        ],

      },

creative_text: {
        type: DataTypes.TEXT,
      
      

      },

custom_code: {
        type: DataTypes.TEXT,
      
      

      },

destination_url: {
        type: DataTypes.TEXT,
      
      

      },

starts_at: {
        type: DataTypes.DATE,
      
      

      },

ends_at: {
        type: DataTypes.DATE,
      
      

      },

impressions_count: {
        type: DataTypes.INTEGER,
      
      

      },

clicks_count: {
        type: DataTypes.INTEGER,
      
      

      },

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

  ads.associate = (db) => {


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































    db.ads.hasMany(db.ad_events, {
      as: 'ad_events_ad',
      foreignKey: {
          name: 'adId',
      },
      constraints: false,
    });












//end loop



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



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


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

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



  return ads;
};


