// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2

package community

import (
	"context"

	"juwan-backend/app/community/api/internal/svc"
	"juwan-backend/app/community/api/internal/types"
	communitypb "juwan-backend/app/community/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetPostLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取帖子详情
func NewGetPostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPostLogic {
	return &GetPostLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetPostLogic) GetPost(req *types.PathId) (resp *types.Post, err error) {
	out, err := l.svcCtx.CommunityRpc.GetPostsById(l.ctx, &communitypb.GetPostsByIdReq{Id: req.Id})
	if err != nil {
		return nil, err
	}
	uid := currentUserIDOrZero(l.ctx)
	liked := false
	if uid > 0 {
		likes, e := l.svcCtx.CommunityRpc.SearchPostLikes(l.ctx, &communitypb.SearchPostLikesReq{Offset: 0, Limit: 1, PostId: &req.Id, UserId: &uid})
		liked = e == nil && len(likes.GetPostLikes()) > 0
	}
	post := mapPost(l.ctx, l.svcCtx, out.GetPosts(), liked)
	return &post, nil

}
