func (c *Chat) New(ctx context.Context, req *pb.NewRequest, rsp *pb.NewResponse) error {
	if len(req.UserIds) == 0 {
		return errors.BadRequest("chat.New.MissingUserIDs", "One or more user IDs are required")
	}
	sortedIDs := make([]string, len(req.UserIds))
	copy(sortedIDs, req.UserIds)
	sort.Strings(sortedIDs)
	key := chatStoreKeyPrefix + strings.Join(sortedIDs, "_")
	recs, err := store.Read(key)
	if err == nil {
		rsp.ChatId = string(recs[0].Value)
		return nil
	} else if err != store.ErrNotFound {
		logger.Errorf("Error reading from the store. Key: %v. Error: %v", key, err)
		return errors.InternalServerError("chat.New.Unknown", "Error reading from the store")
	}
	chatID := uuid.New().String()
	if err := store.Write(&store.Record{Key: key, Value: []byte(chatID)}); err != nil {
		logger.Errorf("Error writing to the store. Key: %v. Error: %v", key, err)
		return errors.InternalServerError("chat.New.Unknown", "Error writing to the store")
	}
	if err := store.Write(&store.Record{Key: chatStoreKeyPrefix + chatID}); err != nil {
		logger.Errorf("Error writing to the store. Key: %v. Error: %v", chatStoreKeyPrefix+chatID, err)
		return errors.InternalServerError("chat.New.Unknown", "Error writing to the store")
	}
	logger.Infof("New chat created with ID %v", chatID)
	rsp.ChatId = chatID
	return nil
}
New creates a chat for a group of users. The RPC is idempotent so if it's called multiple times
for the same users, the same response will be returned. It's good practice to design APIs as
idempotent since this enables safe retries.
package handler
import (
	"context"
	"sort"
	"strings"
	"time"
	"github.com/google/uuid"
	"github.com/micro/micro/v3/service/context/metadata"
	"github.com/micro/micro/v3/service/errors"
	"github.com/micro/micro/v3/service/events"
	"github.com/micro/micro/v3/service/logger"
	"github.com/micro/micro/v3/service/store"
	"google.golang.org/protobuf/types/known/timestamppb"
	pb "github.com/micro/services/test/chat/proto"
)
const (
	chatStoreKeyPrefix    = "chats/"
	chatEventKeyPrefix    = "chats/"
	messageStoreKeyPrefix = "messages/"
)
type Chat struct{}