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,
      
      

      },

      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.users, {
      as: 'users_schools',
      foreignKey: {
          name: 'schoolsId',
      },
      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.classrooms, {
      as: 'classrooms_school',
      foreignKey: {
          name: 'schoolId',
      },
      constraints: false,
    });


    db.schools.hasMany(db.classroom_memberships, {
      as: 'classroom_memberships_schools',
      foreignKey: {
          name: 'schoolsId',
      },
      constraints: false,
    });


    db.schools.hasMany(db.teaching_assignments, {
      as: 'teaching_assignments_schools',
      foreignKey: {
          name: 'schoolsId',
      },
      constraints: false,
    });


    db.schools.hasMany(db.homeworks, {
      as: 'homeworks_schools',
      foreignKey: {
          name: 'schoolsId',
      },
      constraints: false,
    });


    db.schools.hasMany(db.homework_rubrics, {
      as: 'homework_rubrics_schools',
      foreignKey: {
          name: 'schoolsId',
      },
      constraints: false,
    });


    db.schools.hasMany(db.homework_extensions, {
      as: 'homework_extensions_schools',
      foreignKey: {
          name: 'schoolsId',
      },
      constraints: false,
    });


    db.schools.hasMany(db.homework_submissions, {
      as: 'homework_submissions_schools',
      foreignKey: {
          name: 'schoolsId',
      },
      constraints: false,
    });


    db.schools.hasMany(db.submission_grades, {
      as: 'submission_grades_schools',
      foreignKey: {
          name: 'schoolsId',
      },
      constraints: false,
    });


    db.schools.hasMany(db.rubric_scores, {
      as: 'rubric_scores_schools',
      foreignKey: {
          name: 'schoolsId',
      },
      constraints: false,
    });


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


    db.schools.hasMany(db.gradebook_columns, {
      as: 'gradebook_columns_schools',
      foreignKey: {
          name: 'schoolsId',
      },
      constraints: false,
    });


    db.schools.hasMany(db.gradebook_entries, {
      as: 'gradebook_entries_schools',
      foreignKey: {
          name: 'schoolsId',
      },
      constraints: false,
    });


    db.schools.hasMany(db.adviser_reports, {
      as: 'adviser_reports_schools',
      foreignKey: {
          name: 'schoolsId',
      },
      constraints: false,
    });


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


    db.schools.hasMany(db.notifications, {
      as: 'notifications_schools',
      foreignKey: {
          name: 'schoolsId',
      },
      constraints: false,
    });


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


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


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


    db.schools.hasMany(db.support_settings, {
      as: 'support_settings_schools',
      foreignKey: {
          name: 'schoolsId',
      },
      constraints: false,
    });


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


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


    db.schools.hasMany(db.ai_monitoring_rules, {
      as: 'ai_monitoring_rules_schools',
      foreignKey: {
          name: 'schoolsId',
      },
      constraints: false,
    });



//end loop






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

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



  return schools;
};


