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

title: {
        type: DataTypes.TEXT,
      
      

      },

announced_at: {
        type: DataTypes.DATE,
      
      

      },

body: {
        type: DataTypes.TEXT,
      
      

      },

source_url: {
        type: DataTypes.TEXT,
      
      

      },

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

  ir_announcements.associate = (db) => {


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





































//end loop



    db.ir_announcements.belongsTo(db.ir_companies, {
      as: 'ir_company',
      foreignKey: {
        name: 'ir_companyId',
      },
      constraints: false,
    });



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


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

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



  return ir_announcements;
};


