package agentv1

import (
	"encoding/json"

	"google.golang.org/grpc/encoding"
)

// CodecName is the gRPC content-subtype used by the agent contract. Because this
// repository has no protoc pipeline yet, messages are encoded as JSON rather than
// protobuf wire format. Clients select it with grpc.CallContentSubtype(CodecName)
// (wired automatically by the client/server helpers in service.go); the server
// echoes the same subtype on responses.
const CodecName = "pangolinagentjson"

func init() { encoding.RegisterCodec(jsonCodec{}) }

// jsonCodec implements google.golang.org/grpc/encoding.Codec over encoding/json.
type jsonCodec struct{}

func (jsonCodec) Marshal(v any) ([]byte, error) { return json.Marshal(v) }

func (jsonCodec) Unmarshal(data []byte, v any) error {
	if len(data) == 0 {
		return nil // empty message (e.g. AckResponse / UsageAck)
	}
	return json.Unmarshal(data, v)
}

func (jsonCodec) Name() string { return CodecName }
