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

employee_number: {
        type: DataTypes.TEXT,
      
      

      },

job_title: {
        type: DataTypes.TEXT,
      
      

      },

staff_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"teacher",


"admin",


"support"

        ],

      },

hire_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"on_leave",


"inactive"

        ],

      },

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

  staff.associate = (db) => {


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













    db.staff.hasMany(db.classes, {
      as: 'classes_homeroom_teacher',
      foreignKey: {
          name: 'homeroom_teacherId',
      },
      constraints: false,
    });



    db.staff.hasMany(db.class_subjects, {
      as: 'class_subjects_teacher',
      foreignKey: {
          name: 'teacherId',
      },
      constraints: false,
    });




    db.staff.hasMany(db.attendance_sessions, {
      as: 'attendance_sessions_taken_by',
      foreignKey: {
          name: 'taken_byId',
      },
      constraints: false,
    });





    db.staff.hasMany(db.payments, {
      as: 'payments_received_by',
      foreignKey: {
          name: 'received_byId',
      },
      constraints: false,
    });








//end loop



    db.staff.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.staff.belongsTo(db.campuses, {
      as: 'campus',
      foreignKey: {
        name: 'campusId',
      },
      constraints: false,
    });

    db.staff.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });



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


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

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



  return staff;
};


