"use strict";

const { isValidPassword, generateHashPassword } = require("../../helpers/security.helper");

module.exports = function (sequelize, DataTypes) {
  const User = sequelize.define(
    "User",
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      phone: {
        type: DataTypes.STRING,
        unique: true,
        allowNull: true, //Tiene que poderse dar de alta usuarios solo con el correo electronico para que siga funcionando las invitaciones por web como hasta ahora
      },
      email: {
        type: DataTypes.STRING,
        unique: true,
        //            allowNull: false,
        //            validate: { isEmail: true }
      },
      password: {
        type: DataTypes.STRING,
        //            allowNull: false,
      },
      fbuid: {
        type: DataTypes.STRING,
      },
      name: {
        type: DataTypes.STRING,
      },
      surname: {
        type: DataTypes.STRING,
      },
      profile_picture: {
        type: DataTypes.STRING,
        defaultValue: "media/defaultProfile.png",
      },
      accessibility: {
        type: DataTypes.BOOLEAN,
        defaultValue: true,
      },
      profile: {
        type: DataTypes.STRING,
      },
      refresh_token: {
        type: DataTypes.STRING(512),
      },
      state: {
        type: DataTypes.STRING,
        defaultValue: "active",
      },
      app_version: {
        type: DataTypes.STRING,
      },
      level: {
        type: DataTypes.INTEGER,
        defaultValue: 1,
      },
      lastlogin: {
        type: DataTypes.DATE,
        defaultValue: null,
      },
      country: {
        type: DataTypes.STRING,
        defaultValue: "ES",
      },
    },
    {
      tableName: "users",
      freezeTableName: true,
      timestamps: true,
    }
  );

  User.associate = function (models) {
    User.Roles = User.belongsToMany(models.Rol, {
      through: models.UserRoles,
      foreignKey: "userId",
      as: "roles",
    });
    User.Entity = User.belongsTo(models.Entity, { foreignKey: "entityId" });
    User.EventsCreates = User.hasMany(models.Event, { foreignKey: "userId" });
    User.Devices = User.hasMany(models.UserDevice, { foreignKey: "userId" });
    User.Comments = User.hasMany(models.Comment, { foreignKey: "userId" });
    User.EventsReservations = User.hasMany(models.EventReservation, {
      foreignKey: "userId",
    });
    User.EventsInscriptions = User.hasMany(models.EventInscription, {
      foreignKey: "userId",
    });
    User.Questions = User.hasMany(models.EventQuestion, {
      foreignKey: "userId",
      as: "questions",
      required: false,
    });

    //        User.InscriptionsValidate = User.hasMany(models.EventIncription, { foreignkey: 'validateUserId'})
    //User.Reactions = User.hasMany(models.UserReaction, { foreignKey: 'UserId' });
  };

  User.beforeCreate(async function (model, options) {
    if (model.password) {
      const encrypted = await generateHashPassword(model.password);
      model.password = encrypted;
    }
    return model;
  });

  // Instance Methods
  // InventoryLevel.prototype.someMethod = function () {...}

  User.prototype.comparePassword = async function (candidatePassword) {
    const user = this;
    if (user.password) {
      return await isValidPassword(user.password, candidatePassword);
    } else {
      return false;
    }
  };

  return User;
};
