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

requested_role: {
        type: DataTypes.ENUM,
      
      

        values: [

"student",


"teacher",


"principal"

        ],

      },

application_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"approved",


"rejected",


"needs_more_info"

        ],

      },

application_notes: {
        type: DataTypes.TEXT,
      
      

      },

reviewed_at: {
        type: DataTypes.DATE,
      
      

      },

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

  account_applications.associate = (db) => {


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































//end loop



    db.account_applications.belongsTo(db.users, {
      as: 'applicant',
      foreignKey: {
        name: 'applicantId',
      },
      constraints: false,
    });

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

    db.account_applications.belongsTo(db.users, {
      as: 'reviewed_by',
      foreignKey: {
        name: 'reviewed_byId',
      },
      constraints: false,
    });



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


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

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



  return account_applications;
};


