package com.yaoai.admin.auth;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpLogic;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StpAdminUtil {

    public static final StpLogic INSTANCE = new StpLogic("admin");

    static {
        INSTANCE.setConfig(new SaTokenConfig().setTokenName("satoken-admin"));
        SaManager.putStpLogic(INSTANCE);
    }

    public static void login(Long adminId) {
        INSTANCE.login(adminId);
    }

    public static long getLoginIdAsLong() {
        return INSTANCE.getLoginIdAsLong();
    }

    public static String getTokenValue() {
        return INSTANCE.getTokenValue();
    }

    public static void logout() {
        INSTANCE.logout();
    }

    public static boolean isLogin() {
        return INSTANCE.isLogin();
    }

    public static SaSession getSession() {
        return INSTANCE.getSession();
    }

    public static String getRole() {
        List<String> roles = INSTANCE.getRoleList();
        return roles.isEmpty() ? "default-role" : roles.get(0);
    }
}
