package dev.service;

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

public class AuthService {

    UserDao userDao = new UserDao();

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

        // Validate and register user
        if( username == null || username.isBlank() || password == null || password.isBlank())
            return false;

        if (userDao.findByUsername(username) == null) {

            User user = new User();

            user.setEmail(email);
            user.setPassword(password);
            user.setUsername(username);

            return userDao.save(user);
        }
        return false;
    }

    public User login(String username, String password) {

        // Authenticate user
        User user = userDao.findByUsername(username);

        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }

}