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

name: {
        type: DataTypes.TEXT,
      
      

      },

growth_days: {
        type: DataTypes.INTEGER,
      
      

      },

regrowth_days: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

preferred_season: {
        type: DataTypes.ENUM,
      
      

        values: [

"summer",


"autumn",


"winter",


"spring",


"wet",


"dry",


"any"

        ],

      },

water_per_day: {
        type: DataTypes.INTEGER,
      
      

      },

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

  crops.associate = (db) => {


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







































    db.crops.hasMany(db.plantings, {
      as: 'plantings_crop',
      foreignKey: {
          name: 'cropId',
      },
      constraints: false,
    });













//end loop



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

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



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


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

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



  return crops;
};


