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

announcement_title: {
        type: DataTypes.TEXT,
      
      

      },

announcement_body: {
        type: DataTypes.TEXT,
      
      

      },

audience_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"school",


"classroom",


"role_based",


"global"

        ],

      },

target_role: {
        type: DataTypes.ENUM,
      
      

        values: [

"student",


"teacher",


"principal",


"admin"

        ],

      },

posted_at: {
        type: DataTypes.DATE,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  announcements.associate = (db) => {


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






















    db.announcements.hasMany(db.notifications, {
      as: 'notifications_announcement',
      foreignKey: {
          name: 'announcementId',
      },
      constraints: false,
    });










//end loop



    db.announcements.belongsTo(db.schools, {
      as: 'school',
      foreignKey: {
        name: 'schoolId',
      },
      constraints: false,
    });

    db.announcements.belongsTo(db.classrooms, {
      as: 'classroom',
      foreignKey: {
        name: 'classroomId',
      },
      constraints: false,
    });

    db.announcements.belongsTo(db.users, {
      as: 'posted_by',
      foreignKey: {
        name: 'posted_byId',
      },
      constraints: false,
    });



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


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

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



  return announcements;
};


