package com.home4j.service;

import com.home4j.entity.User;
import io.quarkus.elytron.security.common.BcryptUtil;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.transaction.Transactional;

/**
 * 用户服务
 */
@ApplicationScoped
public class UserService {

    /**
     * 创建用户（密码会自动BCrypt加密）
     */
    @Transactional
    public User createUser(String username, String password) {
        User user = new User();
        user.username = username;
        user.password = BcryptUtil.bcryptHash(password);
        user.role = "ADMIN";
        user.persist();
        return user;
    }

    /**
     * 更新密码
     */
    @Transactional
    public void updatePassword(Long userId, String newPassword) {
        User user = User.findById(userId);
        if (user != null) {
            user.password = BcryptUtil.bcryptHash(newPassword);
            user.persist();
        }
    }

    /**
     * 检查是否存在任何用户
     */
    public boolean hasAnyUser() {
        return User.count() > 0;
    }

    /**
     * 获取用户数量
     */
    public long getUserCount() {
        return User.count();
    }
}
