package dev.service;

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

public class AuthService
{
    private final UserDao userDao = new UserDao();

    public boolean register(String username, String password, String email)
    {
       if (userDao.findByUsername(username) != null)
       {
           System.out.println("username exists");
           return false;
       }

       password = Integer.toHexString(password.hashCode());
       User user = new User();
       user.setUsername(username);
       user.setPassword(password);
       user.setEmail(email);
       return userDao.save(user);
    }

    public User login(String username, String password)
    {
        User user = userDao.findByUsername(username);

        if (user == null) return null;

        String hash = Integer.toHexString(password.hashCode());

        if(user.getPassword().equals(hash)) return user;

        return null;
    }
}