package com.datamate.common.auth.infrastructure.context;

import lombok.Getter;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 请求级用户上下文
 */
@Getter
public class RequestUserContext {
    private final String userId;
    private final String username;
    private final List<String> roles;

    private RequestUserContext(String userId, String username, List<String> roles) {
        this.userId = userId;
        this.username = username;
        this.roles = roles == null ? Collections.emptyList() : List.copyOf(roles);
    }

    public static RequestUserContext of(String userId, String username, List<String> roles) {
        return new RequestUserContext(userId, username, roles);
    }

    public static RequestUserContext empty() {
        return new RequestUserContext(null, null, Collections.emptyList());
    }

    public boolean hasRole(String roleCode) {
        if (!StringUtils.hasText(roleCode)) {
            return false;
        }
        return roles.stream().anyMatch(role -> StringUtils.hasText(role) && Objects.equals(role.trim(), roleCode));
    }
}

