package logic

import (
	"context"
	"errors"

	"juwan-backend/app/chat/rpc/internal/svc"
	"juwan-backend/app/chat/rpc/pb"

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

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

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

func (l *GetChatMessagesByIdLogic) GetChatMessagesById(in *pb.GetChatMessagesByIdReq) (*pb.GetChatMessagesByIdResp, error) {
	store := l.svcCtx.Store
	store.Mu.RLock()
	defer store.Mu.RUnlock()

	msg, ok := store.Messages[in.GetId()]
	if !ok {
		return nil, errors.New("message not found")
	}

	return &pb.GetChatMessagesByIdResp{ChatMessages: msg}, nil
}
