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

name: {
        type: DataTypes.TEXT,
      
      

      },

item_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"product",


"ingredient",


"seed",


"fish",


"crop",


"resource",


"tool",


"furniture",


"decoration",


"consumable",


"quest"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

base_price: {
        type: DataTypes.DECIMAL,
      
      

      },

weight: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

max_stack_size: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

shelf_life_days: {
        type: DataTypes.INTEGER,
      
      

      },

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

  items.associate = (db) => {


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



















    db.items.hasMany(db.quest_objectives, {
      as: 'quest_objectives_target_item',
      foreignKey: {
          name: 'target_itemId',
      },
      constraints: false,
    });





    db.items.hasMany(db.store_products, {
      as: 'store_products_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });




    db.items.hasMany(db.purchase_order_lines, {
      as: 'purchase_order_lines_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });











    db.items.hasMany(db.price_modifiers, {
      as: 'price_modifiers_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });


    db.items.hasMany(db.crops, {
      as: 'crops_seed_item',
      foreignKey: {
          name: 'seed_itemId',
      },
      constraints: false,
    });

    db.items.hasMany(db.crops, {
      as: 'crops_harvest_item',
      foreignKey: {
          name: 'harvest_itemId',
      },
      constraints: false,
    });




    db.items.hasMany(db.fish_species, {
      as: 'fish_species_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });




    db.items.hasMany(db.crafting_recipes, {
      as: 'crafting_recipes_output_item',
      foreignKey: {
          name: 'output_itemId',
      },
      constraints: false,
    });


    db.items.hasMany(db.recipe_ingredients, {
      as: 'recipe_ingredients_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });








//end loop



    db.items.belongsTo(db.item_categories, {
      as: 'category',
      foreignKey: {
        name: 'categoryId',
      },
      constraints: false,
    });



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


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

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



  return items;
};


