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

title: {
        type: DataTypes.TEXT,
      
      

      },

quest_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"story",


"community",


"market",


"fishing",


"environmental",


"hidden",


"random_event"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"completed",


"failed"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

available_from: {
        type: DataTypes.DATE,
      
      

      },

available_until: {
        type: DataTypes.DATE,
      
      

      },

reward_money: {
        type: DataTypes.DECIMAL,
      
      

      },

reward_reputation: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  quests.associate = (db) => {


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



















    db.quests.hasMany(db.quest_objectives, {
      as: 'quest_objectives_quest',
      foreignKey: {
          name: 'questId',
      },
      constraints: false,
    });

































//end loop



    db.quests.belongsTo(db.npcs, {
      as: 'giver_npc',
      foreignKey: {
        name: 'giver_npcId',
      },
      constraints: false,
    });

    db.quests.belongsTo(db.locations, {
      as: 'start_location',
      foreignKey: {
        name: 'start_locationId',
      },
      constraints: false,
    });

    db.quests.belongsTo(db.locations, {
      as: 'end_location',
      foreignKey: {
        name: 'end_locationId',
      },
      constraints: false,
    });




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

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



  return quests;
};


