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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  campuses.associate = (db) => {


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











    db.campuses.hasMany(db.rooms, {
      as: 'rooms_campus',
      foreignKey: {
          name: 'campusId',
      },
      constraints: false,
    });


    db.campuses.hasMany(db.students, {
      as: 'students_campus',
      foreignKey: {
          name: 'campusId',
      },
      constraints: false,
    });




    db.campuses.hasMany(db.teachers, {
      as: 'teachers_campus',
      foreignKey: {
          name: 'campusId',
      },
      constraints: false,
    });


    db.campuses.hasMany(db.classes, {
      as: 'classes_campus',
      foreignKey: {
          name: 'campusId',
      },
      constraints: false,
    });











    db.campuses.hasMany(db.announcements, {
      as: 'announcements_campus',
      foreignKey: {
          name: 'campusId',
      },
      constraints: false,
    });




    db.campuses.hasMany(db.events, {
      as: 'events_campus',
      foreignKey: {
          name: 'campusId',
      },
      constraints: false,
    });



//end loop



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




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

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



  return campuses;
};


