package logic

import (
	"context"
	"errors"

	"juwan-backend/app/community/rpc/internal/models/comments"
	"juwan-backend/app/community/rpc/internal/svc"
	"juwan-backend/app/community/rpc/pb"

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

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

func NewGetCommentsByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCommentsByIdLogic {
	return &GetCommentsByIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetCommentsByIdLogic) GetCommentsById(in *pb.GetCommentsByIdReq) (*pb.GetCommentsByIdResp, error) {
	if in.GetId() <= 0 {
		return nil, errors.New("id is required")
	}

	comment, err := l.svcCtx.CommunityModelRO.Comments.Query().
		Where(comments.IDEQ(in.GetId()), comments.DeletedAtIsNil()).
		First(l.ctx)
	if err != nil {
		return nil, errors.New("comment not found")
	}

	return &pb.GetCommentsByIdResp{Comments: entCommentToPb(comment)}, nil
}
