package com.snack.server.config;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.core.util.StrUtil;
import com.snack.server.module.admin.entity.Admin;
import com.snack.server.module.admin.mapper.AdminMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

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

/**
 * Sa-Token 角色数据源：从 admin 表读取当前登录管理员的角色。
 */
@Component
@RequiredArgsConstructor
public class StpInterfaceImpl implements StpInterface {

    private final AdminMapper adminMapper;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        return Collections.emptyList();
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        if (loginId == null) {
            return Collections.emptyList();
        }
        try {
            Admin admin = adminMapper.selectById(Long.valueOf(loginId.toString()));
            if (admin != null && StrUtil.isNotBlank(admin.getRole())) {
                return Collections.singletonList(admin.getRole());
            }
        } catch (NumberFormatException ignored) {
            // 非数值登录 ID 不属于管理员
        }
        return Collections.emptyList();
    }
}
