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

campaign_name: {
        type: DataTypes.TEXT,
      
      

      },

channel: {
        type: DataTypes.ENUM,
      
      

        values: [

"sms",


"email",


"whatsapp",


"push"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"scheduled",


"sent",


"cancelled"

        ],

      },

scheduled_at: {
        type: DataTypes.DATE,
      
      

      },

sent_at: {
        type: DataTypes.DATE,
      
      

      },

message_body: {
        type: DataTypes.TEXT,
      
      

      },

segment_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"all",


"by_branch",


"high_value",


"inactive",


"custom"

        ],

      },

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

  marketing_campaigns.associate = (db) => {


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























































    db.marketing_campaigns.hasMany(db.campaign_recipients, {
      as: 'campaign_recipients_campaign',
      foreignKey: {
          name: 'campaignId',
      },
      constraints: false,
    });




//end loop



    db.marketing_campaigns.belongsTo(db.branches, {
      as: 'branch',
      foreignKey: {
        name: 'branchId',
      },
      constraints: false,
    });

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




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

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



  return marketing_campaigns;
};


