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) {

        // TODO:
        // Validate and register user
        if(userDao.findByUsername(username) != null)
        {
            System.out.println("Username is already taken.");
            return false;
        }
        return userDao.save(new User(username, password, email));
    }

    public User login(String username, String password) {

        // TODO:
        // Authenticate user
        User user = userDao.findByUsername(username);
        if(user == null)
        {
            System.out.println("Invalid username or password.");
            return null;
        }

        boolean isPasswordCorrect = BCrypt.checkpw(password, user.getPassword());
        if (!isPasswordCorrect) {
            System.out.println("Invalid password.");
            return null;
        }
        System.out.println("Login successful!");
        return user;
    }

    }