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

mission_title: {
        type: DataTypes.TEXT,
      
      

      },

mission_code: {
        type: DataTypes.TEXT,
      
      

      },

mission_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"video",


"document",


"quiz",


"simulation",


"task",


"checklist_item"

        ],

      },

sequence_order: {
        type: DataTypes.INTEGER,
      
      

      },

estimated_minutes: {
        type: DataTypes.INTEGER,
      
      

      },

instructions: {
        type: DataTypes.TEXT,
      
      

      },

video_url: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

points_awarded: {
        type: DataTypes.DECIMAL,
      
      

      },

coins_awarded: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"archived"

        ],

      },

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

  missions.associate = (db) => {


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
















    db.missions.hasMany(db.mission_attempts, {
      as: 'mission_attempts_mission',
      foreignKey: {
          name: 'missionId',
      },
      constraints: false,
    });


    db.missions.hasMany(db.quizzes, {
      as: 'quizzes_mission',
      foreignKey: {
          name: 'missionId',
      },
      constraints: false,
    });






    db.missions.hasMany(db.simulations, {
      as: 'simulations_mission',
      foreignKey: {
          name: 'missionId',
      },
      constraints: false,
    });


























//end loop



    db.missions.belongsTo(db.learning_paths, {
      as: 'learning_path',
      foreignKey: {
        name: 'learning_pathId',
      },
      constraints: false,
    });

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



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


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

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



  return missions;
};


