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

title: {
        type: DataTypes.TEXT,
      
      

      },

goal: {
        type: DataTypes.TEXT,
      
      

      },

start_date: {
        type: DataTypes.DATE,
      
      

      },

end_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"paused",


"completed"

        ],

      },

target_hours_per_week: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  study_plans.associate = (db) => {


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






























    db.study_plans.hasMany(db.study_plan_items, {
      as: 'study_plan_items_study_plan',
      foreignKey: {
          name: 'study_planId',
      },
      constraints: false,
    });




//end loop



    db.study_plans.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

    db.study_plans.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      constraints: false,
    });

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




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

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



  return study_plans;
};


