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

name: {
        type: DataTypes.TEXT,
      
      

      },

starts_at: {
        type: DataTypes.DATE,
      
      

      },

ends_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  school_years.associate = (db) => {


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














    db.school_years.hasMany(db.class_sections, {
      as: 'class_sections_school_year',
      foreignKey: {
          name: 'school_yearId',
      },
      constraints: false,
    });



    db.school_years.hasMany(db.terms, {
      as: 'terms_school_year',
      foreignKey: {
          name: 'school_yearId',
      },
      constraints: false,
    });







    db.school_years.hasMany(db.exams, {
      as: 'exams_school_year',
      foreignKey: {
          name: 'school_yearId',
      },
      constraints: false,
    });













    db.school_years.hasMany(db.school_statistics, {
      as: 'school_statistics_school_year',
      foreignKey: {
          name: 'school_yearId',
      },
      constraints: false,
    });





//end loop






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

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



  return school_years;
};


