package system

import (
	"kra/internal/biz/system"
	"kra/internal/server/middleware"

	"github.com/go-kratos/kratos/v2/errors"
	"github.com/go-kratos/kratos/v2/transport/http"
)

// JwtBlacklistService JWT黑名单服务
type JwtBlacklistService struct {
	uc *system.JwtBlacklistUsecase
}

// NewJwtBlacklistService 创建JWT黑名单服务
func NewJwtBlacklistService(uc *system.JwtBlacklistUsecase) *JwtBlacklistService {
	return &JwtBlacklistService{uc: uc}
}

// JoinBlacklist 加入黑名单（登出时调用）
func (s *JwtBlacklistService) JoinBlacklist(ctx http.Context) error {
	token := middleware.GetToken(ctx)
	if token == "" {
		return errors.BadRequest("TOKEN_EMPTY", "token为空")
	}
	if err := s.uc.JoinBlacklist(ctx, token); err != nil {
		return errors.InternalServer("BLACKLIST_ERROR", err.Error())
	}
	return ctx.Result(200, map[string]any{
		"code": 0,
		"msg":  "登出成功",
	})
}

// RegisterRoutes 注册路由
func (s *JwtBlacklistService) RegisterRoutes(rg *RouterGroup) {
	// JWT黑名单（不需要操作记录）
	rg.Private.POST("/jwt/jsonInBlacklist", s.JoinBlacklist)
}
