package marketing.heyday.hartmann.fotodocumentation.core.service;

import java.util.Optional;

import jakarta.ejb.LocalBean;
import jakarta.ejb.Stateless;
import jakarta.inject.Inject;
import marketing.heyday.hartmann.fotodocumentation.core.model.User;
import marketing.heyday.hartmann.fotodocumentation.core.query.Param;
import marketing.heyday.hartmann.fotodocumentation.rest.vo.TokenPairValue;

/**
 * 
 * <p>Copyright: Copyright (c) 2024</p>
 * <p>Company: heyday Marketing GmbH</p>
 * @author <a href="mailto:p.verboom@heyday.marketing">Patrick Verboom</a>
 * @version 1.0
 * 
 * created: 21 Jan 2026
 */
@Stateless
@LocalBean
public class LoginService extends AbstractService{
	
	@Inject
	private JwtTokenService jwtTokenService;

	public TokenPairValue authenticateUser(String username, String deviceInfo, String ipAddress) {
		// Get logged-in user from database
		Optional<User> userOpt = queryService.callNamedQuerySingleResult(User.BY_USERNAME, new Param(User.PARAM_USERNAME, username));
		if (userOpt.isEmpty()) {
			// Should never happen
			return null;
		}

		User user = userOpt.get();
		return jwtTokenService.generateTokenPair(user, deviceInfo, ipAddress);
	}
}
