package dev.service;

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

public class AuthService
{
    private final UserDao userDao;

    public AuthService(UserDao userDao) {this.userDao = userDao;}

    public boolean register(String username, String password, String email)
    {
        if (username == null || username.isBlank()) return false;
        if (password == null || password.length() < 4) return false;

        if (userDao.findByUsername(username) != null)
        {
            System.out.println("Username already exists!");
            return false;
        }

        User user = new User();
        user.setUsername(username);

        user.setPassword(password);

        user.setEmail(email);

        return userDao.save(user);
    }

    public User login(String username, String password)
    {
        if (username == null || password == null) return null;

        User user = userDao.findByUsername(username);

        if (user == null)
        {
            System.out.println("User not found!");
            return null;
        }

        if (!user.getPassword().equals(password))
        {
            System.out.println("Wrong password!");
            return null;
        }

        return user;
    }
}