package dev.service;

import dev.dao.UserDao;
import dev.model.User;
import org.mindrot.jbcrypt.BCrypt;

public class AuthService {

    private UserDao userDao = new UserDao();

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

        if(userDao.findByUsername(username) == null){
            User user = new User (username , password , email);
            if(userDao.save(user)){
                System.out.println("successfully register");
                return true;
            }else {
                System.out.println("register failed");
                return false;
            }
        }
        else {
            System.out.println("Username already exists.");
            return false;
        }

    }

    public User login(String username, String password) {

        User user = userDao.findByUsername(username);

        if(user == null){
            System.out.println("Username not found.");
            return null;
        }else {
            String storedHash = user.getPassword();

            if (BCrypt.checkpw(password, storedHash)) {
                return user;

            } else {
                System.out.println("Incorrect password.");
                return null;
            }
        }

    }

}