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

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"in_progress",


"paused",


"completed",


"failed",


"cancelled"

        ],

      },

total_files: {
        type: DataTypes.INTEGER,
      
      

      },

completed_files: {
        type: DataTypes.INTEGER,
      
      

      },

failed_files: {
        type: DataTypes.INTEGER,
      
      

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

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

  upload_sessions.associate = (db) => {

    db.upload_sessions.belongsToMany(db.upload_files, {
      as: 'upload_files',
      foreignKey: {
        name: 'upload_sessions_upload_filesId',
      },
      constraints: false,
      through: 'upload_sessionsUpload_filesUpload_files',
    });

    db.upload_sessions.belongsToMany(db.upload_files, {
      as: 'upload_files_filter',
      foreignKey: {
        name: 'upload_sessions_upload_filesId',
      },
      constraints: false,
      through: 'upload_sessionsUpload_filesUpload_files',
    });


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
















    db.upload_sessions.hasMany(db.upload_files, {
      as: 'upload_files_upload_session',
      foreignKey: {
          name: 'upload_sessionId',
      },
      constraints: false,
    });


























//end loop



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

    db.upload_sessions.belongsTo(db.users, {
      as: 'initiator',
      foreignKey: {
        name: 'initiatorId',
      },
      constraints: false,
    });

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




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

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



  return upload_sessions;
};


