package tech.easyflow.ai.chattime.availability;

import tech.easyflow.ai.entity.Bot;
import tech.easyflow.common.entity.LoginAccount;
import tech.easyflow.core.runtime.ChatChannel;
import tech.easyflow.core.runtime.ChatRuntimeContext;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.Map;

/**
 * 聊天时工具可用性判定上下文。
 */
public class ChatTimeToolAvailabilityContext implements Serializable {

    /**
     * 运行时上下文中保存聊天时权限快照的扩展字段 key。
     */
    public static final String RUNTIME_EXT_KEY = "chatTimeToolAvailabilityContext";

    private LoginAccount loginAccount;

    private Bot bot;

    private ChatChannel chatChannel;

    private BigInteger sessionId;

    /**
     * 将当前登录用户快照绑定到运行时上下文。
     *
     * @param runtimeContext 聊天运行时上下文
     * @param loginAccount 登录用户快照
     * @param bot 当前聊天助手
     */
    public static void bindLoggedInSnapshot(ChatRuntimeContext runtimeContext, LoginAccount loginAccount, Bot bot) {
        if (!hasLoggedInAccount(loginAccount)) {
            return;
        }
        ChatTimeToolAvailabilityContext chatTimeContext = new ChatTimeToolAvailabilityContext();
        chatTimeContext.setLoginAccount(loginAccount);
        chatTimeContext.setBot(bot);
        chatTimeContext.setChatChannel(runtimeContext == null ? null : runtimeContext.getChannel());
        chatTimeContext.setSessionId(runtimeContext == null ? null : runtimeContext.getSessionId());
        chatTimeContext.bindToRuntimeContext(runtimeContext);
    }

    /**
     * 判断是否为可用于聊天态权限判定的登录用户。
     *
     * @param loginAccount 登录用户快照
     * @return 是否为有效登录用户
     */
    public static boolean hasLoggedInAccount(LoginAccount loginAccount) {
        return loginAccount != null
                && loginAccount.getId() != null
                && !BigInteger.ZERO.equals(loginAccount.getId());
    }

    /**
     * 绑定到聊天运行时上下文，供异步链路显式透传。
     *
     * @param runtimeContext 聊天运行时上下文
     */
    public void bindToRuntimeContext(ChatRuntimeContext runtimeContext) {
        if (runtimeContext == null) {
            return;
        }
        Map<String, Object> ext = runtimeContext.getExt();
        ext.put(RUNTIME_EXT_KEY, this);
    }

    /**
     * 从聊天运行时上下文中读取聊天时权限上下文。
     *
     * @param runtimeContext 聊天运行时上下文
     * @return 聊天时权限上下文，不存在时返回 null
     */
    public static ChatTimeToolAvailabilityContext fromRuntimeContext(ChatRuntimeContext runtimeContext) {
        if (runtimeContext == null || runtimeContext.getExt() == null) {
            return null;
        }
        Object value = runtimeContext.getExt().get(RUNTIME_EXT_KEY);
        if (value instanceof ChatTimeToolAvailabilityContext context) {
            return context;
        }
        return null;
    }

    public LoginAccount getLoginAccount() {
        return loginAccount;
    }

    public void setLoginAccount(LoginAccount loginAccount) {
        this.loginAccount = loginAccount;
    }

    public Bot getBot() {
        return bot;
    }

    public void setBot(Bot bot) {
        this.bot = bot;
    }

    public ChatChannel getChatChannel() {
        return chatChannel;
    }

    public void setChatChannel(ChatChannel chatChannel) {
        this.chatChannel = chatChannel;
    }

    public BigInteger getSessionId() {
        return sessionId;
    }

    public void setSessionId(BigInteger sessionId) {
        this.sessionId = sessionId;
    }
}
