const { Model, DataTypes } = require('sequelize');
const bcrypt = require('bcrypt');
const sequelize = require('../config/connection');

class User extends Model {
    checkPassword(loginPassword) {
        return bcrypt.compareSync(loginPassword, this.password)
    };
}

User.init(
    {
        id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        user: {
            type: DataTypes.STRING,
            unique: true,
            allowNull: false,
        },
        password: {
            type: DataTypes.STRING,
            allowNull: false,
        }
    },
    {
        sequelize,
        timestamps: false,
        freezeTableName: true,
        underscored: true,
        modelName: 'user',
        hooks: {
            beforeCreate: async (newUserData) => {
                console.log(newUserData.password)
                newUserData.password = await bcrypt.hash(newUserData.password, 10);
                return newUserData;
            },
            // afterCreate: async (updatedUserData) => {
            //     updatedUserData.password = await bcrypt.hash(updatedUserData.toString(), 10);
            //     return updatedUserData
            // }
        }
    }
);

module.exports = User;