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

title: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"archived"

        ],

      },

starts_at: {
        type: DataTypes.DATE,
      
      

      },

ends_at: {
        type: DataTypes.DATE,
      
      

      },

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

  stories.associate = (db) => {

    db.stories.belongsToMany(db.story_items, {
      as: 'story_items',
      foreignKey: {
        name: 'stories_story_itemsId',
      },
      constraints: false,
      through: 'storiesStory_itemsStory_items',
    });

    db.stories.belongsToMany(db.story_items, {
      as: 'story_items_filter',
      foreignKey: {
        name: 'stories_story_itemsId',
      },
      constraints: false,
      through: 'storiesStory_itemsStory_items',
    });


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





































    db.stories.hasMany(db.story_items, {
      as: 'story_items_story',
      foreignKey: {
          name: 'storyId',
      },
      constraints: false,
    });





//end loop



    db.stories.belongsTo(db.albums, {
      as: 'album',
      foreignKey: {
        name: 'albumId',
      },
      constraints: false,
    });

    db.stories.belongsTo(db.users, {
      as: 'created_by',
      foreignKey: {
        name: 'created_byId',
      },
      constraints: false,
    });

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




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

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



  return stories;
};


