package dev.service;

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

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class AuthService {

    public boolean register(String username, String password, String email) {
        if (username == null || username.trim().isEmpty()) {
            System.out.println("Username cannot be empty");
            return false;
        }
        if (password == null || password.trim().isEmpty()) {
            System.out.println("Password cannot be empty");
            return false;
        }

        try {
            UserDao userDao = new UserDao();

            User existingUser = userDao.findByUsername(username);
            if (existingUser != null) {
                System.out.println("Username '" + username + "' is already taken!");
                return false;
            }

            String hashedPassword = hashPassword(password);

            User user = new User();
            user.setUsername(username);
            user.setPassword(hashedPassword);
            user.setEmail(email);

            boolean saved = userDao.save(user);

            if (saved) {
                System.out.println("Registration successful! Welcome " + username + "!");
            } else {
                System.out.println("Registration failed. Please try again.");
            }

            return saved;

        } catch (Exception e) {
            System.err.println("Registration error: " + e.getMessage());
            return false;
        }
    }

    public User login(String username, String password) {
        if (username == null || username.trim().isEmpty()) {
            System.out.println("Username cannot be empty");
            return null;
        }
        if (password == null || password.trim().isEmpty()) {
            System.out.println("Password cannot be empty");
            return null;
        }

        try {
            UserDao userDao = new UserDao();

            User user = userDao.findByUsername(username);

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

            String hashedEnteredPassword = hashPassword(password);

            if (hashedEnteredPassword.equals(user.getPassword())) {
                System.out.println("Login successful! Welcome back " + username + "!");
                return user;
            } else {
                System.out.println("Invalid password!");
                return null;
            }

        } catch (Exception e) {
            System.err.println("Login error: " + e.getMessage());
            return null;
        }
    }

    private String hashPassword(String password) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            byte[] hashBytes = md.digest(password.getBytes());

            StringBuilder sb = new StringBuilder();
            for (byte b : hashBytes) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }
}