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

firstName: {
        type: DataTypes.TEXT,
      
      

      },

lastName: {
        type: DataTypes.TEXT,
      
      

      },

phoneNumber: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

password: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

emailVerificationToken: {
        type: DataTypes.TEXT,
      
      

      },

emailVerificationTokenExpiresAt: {
        type: DataTypes.DATE,
      
      

      },

passwordResetToken: {
        type: DataTypes.TEXT,
      
      

      },

passwordResetTokenExpiresAt: {
        type: DataTypes.DATE,
      
      

      },

provider: {
        type: DataTypes.TEXT,
      
      

      },

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

  users.associate = (db) => {

    db.users.belongsToMany(db.permissions, {
      as: 'custom_permissions',
      foreignKey: {
        name: 'users_custom_permissionsId',
      },
      constraints: false,
      through: 'usersCustom_permissionsPermissions',
    });

    db.users.belongsToMany(db.permissions, {
      as: 'custom_permissions_filter',
      foreignKey: {
        name: 'users_custom_permissionsId',
      },
      constraints: false,
      through: 'usersCustom_permissionsPermissions',
    });


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







    db.users.hasMany(db.identity_verifications, {
      as: 'identity_verifications_user',
      foreignKey: {
          name: 'userId',
      },
      constraints: false,
    });


    db.users.hasMany(db.wallets, {
      as: 'wallets_owner',
      foreignKey: {
          name: 'ownerId',
      },
      constraints: false,
    });




    db.users.hasMany(db.chat_participants, {
      as: 'chat_participants_user',
      foreignKey: {
          name: 'userId',
      },
      constraints: false,
    });


    db.users.hasMany(db.messages, {
      as: 'messages_sender',
      foreignKey: {
          name: 'senderId',
      },
      constraints: false,
    });


    db.users.hasMany(db.stores, {
      as: 'stores_owner',
      foreignKey: {
          name: 'ownerId',
      },
      constraints: false,
    });


    db.users.hasMany(db.addresses, {
      as: 'addresses_user',
      foreignKey: {
          name: 'userId',
      },
      constraints: false,
    });




    db.users.hasMany(db.orders, {
      as: 'orders_buyer',
      foreignKey: {
          name: 'buyerId',
      },
      constraints: false,
    });



    db.users.hasMany(db.payments, {
      as: 'payments_payer',
      foreignKey: {
          name: 'payerId',
      },
      constraints: false,
    });


    db.users.hasMany(db.product_reviews, {
      as: 'product_reviews_buyer',
      foreignKey: {
          name: 'buyerId',
      },
      constraints: false,
    });



    db.users.hasMany(db.posts, {
      as: 'posts_author',
      foreignKey: {
          name: 'authorId',
      },
      constraints: false,
    });


    db.users.hasMany(db.deep_dive_links, {
      as: 'deep_dive_links_creator',
      foreignKey: {
          name: 'creatorId',
      },
      constraints: false,
    });



    db.users.hasMany(db.post_engagements, {
      as: 'post_engagements_user',
      foreignKey: {
          name: 'userId',
      },
      constraints: false,
    });


    db.users.hasMany(db.rewards, {
      as: 'rewards_user',
      foreignKey: {
          name: 'userId',
      },
      constraints: false,
    });


    db.users.hasMany(db.forum_spaces, {
      as: 'forum_spaces_owner',
      foreignKey: {
          name: 'ownerId',
      },
      constraints: false,
    });


    db.users.hasMany(db.forum_threads, {
      as: 'forum_threads_author',
      foreignKey: {
          name: 'authorId',
      },
      constraints: false,
    });


    db.users.hasMany(db.forum_posts, {
      as: 'forum_posts_author',
      foreignKey: {
          name: 'authorId',
      },
      constraints: false,
    });


    db.users.hasMany(db.job_companies, {
      as: 'job_companies_owner',
      foreignKey: {
          name: 'ownerId',
      },
      constraints: false,
    });



    db.users.hasMany(db.job_applications, {
      as: 'job_applications_applicant',
      foreignKey: {
          name: 'applicantId',
      },
      constraints: false,
    });


    db.users.hasMany(db.user_credentials, {
      as: 'user_credentials_user',
      foreignKey: {
          name: 'userId',
      },
      constraints: false,
    });



    db.users.hasMany(db.affiliate_memberships, {
      as: 'affiliate_memberships_member',
      foreignKey: {
          name: 'memberId',
      },
      constraints: false,
    });




    db.users.hasMany(db.facta_queries, {
      as: 'facta_queries_user',
      foreignKey: {
          name: 'userId',
      },
      constraints: false,
    });



    db.users.hasMany(db.subscriptions, {
      as: 'subscriptions_user',
      foreignKey: {
          name: 'userId',
      },
      constraints: false,
    });



    db.users.hasMany(db.audit_events, {
      as: 'audit_events_actor',
      foreignKey: {
          name: 'actorId',
      },
      constraints: false,
    });



//end loop



    db.users.belongsTo(db.roles, {
      as: 'app_role',
      foreignKey: {
        name: 'app_roleId',
      },
      constraints: false,
    });

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



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


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

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


    users.beforeCreate((users, options) => {
        users = trimStringFields(users);

    if (users.provider !== providers.LOCAL && Object.values(providers).indexOf(users.provider) > -1) {
        users.emailVerified = true;

        if (!users.password) {
            const password = crypto
                .randomBytes(20)
                .toString('hex');

            const hashedPassword = bcrypt.hashSync(
            password,
            config.bcrypt.saltRounds,
        );

            users.password = hashedPassword
            }
        }
    });

  users.beforeUpdate((users, options) => {
    users = trimStringFields(users);
  });
  

  return users;
};


function trimStringFields(users) {
  users.email = users.email.trim();

  users.firstName = users.firstName
    ? users.firstName.trim()
    : null;

  users.lastName = users.lastName
    ? users.lastName.trim()
    : null;

  return users;
}

