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

matched_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

last_message_at: {
        type: DataTypes.DATE,
      
      

      },

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

  matches.associate = (db) => {

    db.matches.belongsToMany(db.messages, {
      as: 'messages',
      foreignKey: {
        name: 'matches_messagesId',
      },
      constraints: false,
      through: 'matchesMessagesMessages',
    });

    db.matches.belongsToMany(db.messages, {
      as: 'messages_filter',
      foreignKey: {
        name: 'matches_messagesId',
      },
      constraints: false,
      through: 'matchesMessagesMessages',
    });


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













    db.matches.hasMany(db.messages, {
      as: 'messages_match',
      foreignKey: {
          name: 'matchId',
      },
      constraints: false,
    });


    db.matches.hasMany(db.chat_events, {
      as: 'chat_events_match',
      foreignKey: {
          name: 'matchId',
      },
      constraints: false,
    });















//end loop



    db.matches.belongsTo(db.users, {
      as: 'user_a',
      foreignKey: {
        name: 'user_aId',
      },
      constraints: false,
    });

    db.matches.belongsTo(db.users, {
      as: 'user_b',
      foreignKey: {
        name: 'user_bId',
      },
      constraints: false,
    });




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

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



  return matches;
};


