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

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"photoshoot",


"workshop",


"fest",


"trip",


"competition",


"party",


"conference",


"meetup",


"other"

        ],

      },

starts_at: {
        type: DataTypes.DATE,
      
      

      },

ends_at: {
        type: DataTypes.DATE,
      
      

      },

privacy: {
        type: DataTypes.ENUM,
      
      

        values: [

"public",


"unlisted",


"private"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"archived"

        ],

      },

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

  events.associate = (db) => {

    db.events.belongsToMany(db.albums, {
      as: 'albums',
      foreignKey: {
        name: 'events_albumsId',
      },
      constraints: false,
      through: 'eventsAlbumsAlbums',
    });

    db.events.belongsToMany(db.albums, {
      as: 'albums_filter',
      foreignKey: {
        name: 'events_albumsId',
      },
      constraints: false,
      through: 'eventsAlbumsAlbums',
    });


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









    db.events.hasMany(db.albums, {
      as: 'albums_event',
      foreignKey: {
          name: 'eventId',
      },
      constraints: false,
    });
















    db.events.hasMany(db.notifications, {
      as: 'notifications_event',
      foreignKey: {
          name: 'eventId',
      },
      constraints: false,
    });















    db.events.hasMany(db.analytics_events, {
      as: 'analytics_events_event',
      foreignKey: {
          name: 'eventId',
      },
      constraints: false,
    });




//end loop



    db.events.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.events.belongsTo(db.albums, {
      as: 'default_album',
      foreignKey: {
        name: 'default_albumId',
      },
      constraints: false,
    });



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


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

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



  return events;
};


