// Package types defines the v1 transport envelopes carried over the
// wire between the Galaxy client and gateway. Envelope shapes mirror
// the protobuf messages in `gateway/proto/galaxy/gateway/v1/`, but are
// kept as plain Go structs here so the UI client can read and produce
// them without depending on the protobuf runtime in WASM and gomobile
// builds.
//
// Each envelope exposes a SigningFields method that returns the subset
// of fields covered by the v1 signature (see canon.RequestSigningFields,
// canon.ResponseSigningFields, canon.EventSigningFields and
// docs/ARCHITECTURE.md §15). The TraceID field on a request envelope is
// intentionally not part of the request signing input.
package types

import "galaxy/core/canon"

// RequestEnvelope is the full client-side request envelope. PayloadBytes
// is hashed into PayloadHash; PayloadHash and the remaining envelope
// fields above Signature are bound by the v1 request signature.
type RequestEnvelope struct {
	ProtocolVersion string
	DeviceSessionID string
	MessageType     string
	TimestampMS     int64
	RequestID       string
	PayloadBytes    []byte
	PayloadHash     []byte
	Signature       []byte
	TraceID         string
}

// SigningFields projects the envelope onto the canonical request signing
// fields. TraceID is deliberately excluded: the v1 contract does not bind
// TraceID into the request signature.
func (e RequestEnvelope) SigningFields() canon.RequestSigningFields {
	return canon.RequestSigningFields{
		ProtocolVersion: e.ProtocolVersion,
		DeviceSessionID: e.DeviceSessionID,
		MessageType:     e.MessageType,
		TimestampMS:     e.TimestampMS,
		RequestID:       e.RequestID,
		PayloadHash:     e.PayloadHash,
	}
}

// ResponseEnvelope is the full server-side response envelope. PayloadBytes
// is hashed into PayloadHash; PayloadHash and the remaining envelope
// fields above Signature are bound by the v1 response signature.
type ResponseEnvelope struct {
	ProtocolVersion string
	RequestID       string
	TimestampMS     int64
	ResultCode      string
	PayloadBytes    []byte
	PayloadHash     []byte
	Signature       []byte
}

// SigningFields projects the envelope onto the canonical response signing
// fields.
func (e ResponseEnvelope) SigningFields() canon.ResponseSigningFields {
	return canon.ResponseSigningFields{
		ProtocolVersion: e.ProtocolVersion,
		RequestID:       e.RequestID,
		TimestampMS:     e.TimestampMS,
		ResultCode:      e.ResultCode,
		PayloadHash:     e.PayloadHash,
	}
}

// EventEnvelope is the full server-side push event envelope.
type EventEnvelope struct {
	EventType    string
	EventID      string
	TimestampMS  int64
	RequestID    string
	TraceID      string
	PayloadBytes []byte
	PayloadHash  []byte
	Signature    []byte
}

// SigningFields projects the envelope onto the canonical event signing
// fields.
func (e EventEnvelope) SigningFields() canon.EventSigningFields {
	return canon.EventSigningFields{
		EventType:   e.EventType,
		EventID:     e.EventID,
		TimestampMS: e.TimestampMS,
		RequestID:   e.RequestID,
		TraceID:     e.TraceID,
		PayloadHash: e.PayloadHash,
	}
}
