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.activity_logs, {
      as: 'activity_logs_schools',
      foreignKey: {
        name: 'schoolsId',
      },
      constraints: false,
    });

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    //end loop

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

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

  return schools;
};
