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

title: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

privacy: {
        type: DataTypes.ENUM,
      
      

        values: [

"public",


"unlisted",


"private"

        ],

      },

collaboration_mode: {
        type: DataTypes.ENUM,
      
      

        values: [

"owner_only",


"members_can_add",


"anyone_with_link_can_add"

        ],

      },

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

      },

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

      },

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

      },

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

      },

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

  albums.associate = (db) => {

    db.albums.belongsToMany(db.album_collaborators, {
      as: 'collaborators',
      foreignKey: {
        name: 'albums_collaboratorsId',
      },
      constraints: false,
      through: 'albumsCollaboratorsAlbum_collaborators',
    });

    db.albums.belongsToMany(db.album_collaborators, {
      as: 'collaborators_filter',
      foreignKey: {
        name: 'albums_collaboratorsId',
      },
      constraints: false,
      through: 'albumsCollaboratorsAlbum_collaborators',
    });

    db.albums.belongsToMany(db.access_rules, {
      as: 'access_rules',
      foreignKey: {
        name: 'albums_access_rulesId',
      },
      constraints: false,
      through: 'albumsAccess_rulesAccess_rules',
    });

    db.albums.belongsToMany(db.access_rules, {
      as: 'access_rules_filter',
      foreignKey: {
        name: 'albums_access_rulesId',
      },
      constraints: false,
      through: 'albumsAccess_rulesAccess_rules',
    });

    db.albums.belongsToMany(db.shares, {
      as: 'shares',
      foreignKey: {
        name: 'albums_sharesId',
      },
      constraints: false,
      through: 'albumsSharesShares',
    });

    db.albums.belongsToMany(db.shares, {
      as: 'shares_filter',
      foreignKey: {
        name: 'albums_sharesId',
      },
      constraints: false,
      through: 'albumsSharesShares',
    });

    db.albums.belongsToMany(db.stories, {
      as: 'stories',
      foreignKey: {
        name: 'albums_storiesId',
      },
      constraints: false,
      through: 'albumsStoriesStories',
    });

    db.albums.belongsToMany(db.stories, {
      as: 'stories_filter',
      foreignKey: {
        name: 'albums_storiesId',
      },
      constraints: false,
      through: 'albumsStoriesStories',
    });


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








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



    db.albums.hasMany(db.album_collaborators, {
      as: 'album_collaborators_album',
      foreignKey: {
          name: 'albumId',
      },
      constraints: false,
    });


    db.albums.hasMany(db.access_rules, {
      as: 'access_rules_album',
      foreignKey: {
          name: 'albumId',
      },
      constraints: false,
    });


    db.albums.hasMany(db.media_items, {
      as: 'media_items_album',
      foreignKey: {
          name: 'albumId',
      },
      constraints: false,
    });




    db.albums.hasMany(db.upload_sessions, {
      as: 'upload_sessions_album',
      foreignKey: {
          name: 'albumId',
      },
      constraints: false,
    });










    db.albums.hasMany(db.notifications, {
      as: 'notifications_album',
      foreignKey: {
          name: 'albumId',
      },
      constraints: false,
    });



    db.albums.hasMany(db.shares, {
      as: 'shares_album',
      foreignKey: {
          name: 'albumId',
      },
      constraints: false,
    });











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



    db.albums.hasMany(db.analytics_events, {
      as: 'analytics_events_album',
      foreignKey: {
          name: 'albumId',
      },
      constraints: false,
    });




//end loop



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

    db.albums.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });

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



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


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

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



  return albums;
};


