package dev.service;

import dev.dao.UserDao;
import dev.model.User;

import java.util.Objects;

public class AuthService {

    private final UserDao userDao = new UserDao();

    public boolean register(String username, String password, String email) {

        User user = new User(
                username,
                String.valueOf(password.hashCode()),
                email
        );

        if(userDao.save(user)){
            System.out.println("Registered successfully");
            return true;
        }

        return false;

    }

    public User login(String username, String password) {

        User userDB = userDao.findByUsername(username);

        if(userDB == null) return null;

        String userPasswordDB = userDB.getPassword();
        if( Objects.equals( userPasswordDB, String.valueOf(password.hashCode()) ) )
            return userDB;

        return null;
    }

}