package tech.easyflow.ai.entity;

import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Table;
import tech.easyflow.ai.entity.base.KnowledgeShareBase;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * 知识库分享记录。
 */
@Table("tb_knowledge_share")
public class KnowledgeShare extends KnowledgeShareBase {

    @Column(ignore = true)
    private String shareUrl;

    /**
     * 解析授权范围。
     *
     * @return 授权范围集合
     */
    public Set<String> getPermissionScopeSet() {
        if (getPermissionSet() == null || getPermissionSet().isBlank()) {
            return Collections.emptySet();
        }
        Set<String> scopes = new LinkedHashSet<>();
        String[] segments = getPermissionSet().split(",");
        for (String segment : segments) {
            if (segment == null || segment.isBlank()) {
                continue;
            }
            scopes.add(segment.trim().toUpperCase());
        }
        return scopes;
    }

    /**
     * 写入授权范围。
     *
     * @param scopes 授权范围集合
     */
    public void setPermissionScopes(Iterable<String> scopes) {
        StringBuilder builder = new StringBuilder();
        if (scopes != null) {
            for (String scope : scopes) {
                if (scope == null || scope.isBlank()) {
                    continue;
                }
                if (builder.length() > 0) {
                    builder.append(',');
                }
                builder.append(scope.trim().toUpperCase());
            }
        }
        setPermissionSet(builder.toString());
    }

    public String getShareUrl() {
        return shareUrl;
    }

    public void setShareUrl(String shareUrl) {
        this.shareUrl = shareUrl;
    }
}

