package mw

import (
	"context"

	"github.com/rs/zerolog/log"
	"google.golang.org/grpc"
	"google.golang.org/protobuf/encoding/protojson"
	"google.golang.org/protobuf/proto"
)

func Logging(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
	raw, _ := protojson.Marshal((req).(proto.Message))
	log.Debug().Msgf("request: method: %v, req: %s", info.FullMethod, string(raw))

	if resp, err = handler(ctx, req); err != nil {
		log.Debug().Msgf("response: method: %v, err: %s", info.FullMethod, err.Error())
		return
	}
	rawResp, _ := protojson.Marshal((resp).(proto.Message))
	log.Debug().Msgf("response: method: %v, resp: %s", info.FullMethod, string(rawResp))

	return
}
