package com.huimv.employment.service.auth;

import com.huimv.employment.common.exception.BizException;
import com.huimv.employment.common.exception.ErrorCode;
import com.huimv.employment.dao.entity.FeUser;
import com.huimv.employment.dao.mapper.FeUserMapper;
import org.springframework.stereotype.Service;

/**
 * JWT 鉴权后校验用户是否仍有效，并以数据库 {@code user_type} 为准。
 */
@Service
public class AuthUserValidator {

    private static final String STATUS_DISABLED = "disabled";

    private final FeUserMapper feUserMapper;

    public AuthUserValidator(FeUserMapper feUserMapper) {
        this.feUserMapper = feUserMapper;
    }

    /**
     * 校验用户存在且未禁用，返回库内真实身份类型。
     */
    public ValidatedUser validate(Long userId) {
        FeUser user = feUserMapper.selectById(userId);
        if (user == null) {
            throw new BizException(ErrorCode.UNAUTHORIZED);
        }
        if (STATUS_DISABLED.equals(user.getStatus())) {
            throw new BizException(ErrorCode.USER_DISABLED);
        }
        return new ValidatedUser(user.getId(), user.getUserType());
    }

    public static class ValidatedUser {
        private final Long userId;
        private final String userType;

        public ValidatedUser(Long userId, String userType) {
            this.userId = userId;
            this.userType = userType;
        }

        public Long getUserId() {
            return userId;
        }

        public String getUserType() {
            return userType;
        }
    }
}
