module.exports = function (sequelize, DataTypes) {
  const UserDevice = sequelize.define(
    "UserDevice",
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      token: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      valid: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
      },
      invalidated: {
        type: DataTypes.DATE,
      },
      country: {
        type: DataTypes.STRING,
        defaultValue: "ES",
      },
    },
    {
      tableName: "users_devices",
      freezeTableName: true,
      timestamps: true,

      defaultScope: {
        where: {
          valid: true,
        },
      },
    }
  );

  UserDevice.associate = function (models) {
    UserDevice.User = UserDevice.belongsTo(models.User, { foreignKey: "userId", as: "user" });
  };

  return UserDevice;
};
