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

      title: {
        type: DataTypes.TEXT,
      },

      provider: {
        type: DataTypes.TEXT,
      },

      hours: {
        type: DataTypes.DECIMAL,
      },

      compliance_tag: {
        type: DataTypes.TEXT,
      },

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

  learning_courses.associate = (db) => {
    /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

    db.learning_courses.hasMany(db.enrollment_learnings, {
      as: 'enrollment_learnings_course',
      foreignKey: {
        name: 'courseId',
      },
      constraints: false,
    });

    //end loop

    db.learning_courses.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });

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

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

  return learning_courses;
};
