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

name: {
        type: DataTypes.TEXT,
      
      

      },

short_name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

website: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

timezone: {
        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,
    },
  );

  schools.associate = (db) => {


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






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


    db.schools.hasMany(db.academic_years, {
      as: 'academic_years_school',
      foreignKey: {
          name: 'schoolId',
      },
      constraints: false,
    });



    db.schools.hasMany(db.grade_levels, {
      as: 'grade_levels_school',
      foreignKey: {
          name: 'schoolId',
      },
      constraints: false,
    });


    db.schools.hasMany(db.subjects, {
      as: 'subjects_school',
      foreignKey: {
          name: 'schoolId',
      },
      constraints: false,
    });



    db.schools.hasMany(db.students, {
      as: 'students_school',
      foreignKey: {
          name: 'schoolId',
      },
      constraints: false,
    });


    db.schools.hasMany(db.parents, {
      as: 'parents_school',
      foreignKey: {
          name: 'schoolId',
      },
      constraints: false,
    });



    db.schools.hasMany(db.teachers, {
      as: 'teachers_school',
      foreignKey: {
          name: 'schoolId',
      },
      constraints: false,
    });


    db.schools.hasMany(db.classes, {
      as: 'classes_school',
      foreignKey: {
          name: 'schoolId',
      },
      constraints: false,
    });











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


    db.schools.hasMany(db.messages, {
      as: 'messages_school',
      foreignKey: {
          name: 'schoolId',
      },
      constraints: false,
    });



    db.schools.hasMany(db.events, {
      as: 'events_school',
      foreignKey: {
          name: 'schoolId',
      },
      constraints: false,
    });



//end loop





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


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

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



  return schools;
};


