package user

import (
	"context"
	"git.noahlan.cn/n-admin/n-admin-server/api/internal/svc"
	"git.noahlan.cn/n-admin/n-admin-server/api/internal/types"
	"git.noahlan.cn/n-admin/n-admin-server/dal/errx"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
	"git.noahlan.cn/noahlan/ntool-biz/core/jwt"
	"git.noahlan.cn/noahlan/ntool-biz/core/nstatus"
	"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/msg"

	"net/http"
)

type GetCurrentUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	r      *http.Request
}

func NewGetCurrentUserLogic(r *http.Request, ctx context.Context, svcCtx *svc.ServiceContext) *GetCurrentUserLogic {
	return &GetCurrentUserLogic{
		r:      r,
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetCurrentUserLogic) GetCurrentUser() (resp *types.UserInfo, err error) {
	uid := jwt.GetCurrentUserId(l.ctx)
	if uid == 0 {
		return nil, nstatus.NewApiUnauthorizedErr(msg.Msg(errx.StatusUnauthorized))
	}
	rpcResp, err := l.svcCtx.CoreRpc.GetUser(l.ctx, &core.UserReq{ID: uid})
	if err != nil {
		return nil, err
	}
	resp = l.svcCtx.Convert.ConvertUser(l.ctx, rpcResp)

	return
}
