package types_test

import (
	"testing"

	"galaxy/core/canon"
	"galaxy/core/types"

	"github.com/stretchr/testify/assert"
)

func TestRequestEnvelopeSigningFieldsExcludesTraceID(t *testing.T) {
	t.Parallel()

	envelope := types.RequestEnvelope{
		ProtocolVersion: types.ProtocolVersionV1,
		DeviceSessionID: "device-session-1",
		MessageType:     "user.account.get",
		TimestampMS:     1_700_000_000_000,
		RequestID:       "req-1",
		PayloadBytes:    []byte("payload"),
		PayloadHash:     []byte("01234567890123456789012345678901"),
		Signature:       []byte("ignored"),
		TraceID:         "trace-1",
	}

	assert.Equal(t, canon.RequestSigningFields{
		ProtocolVersion: envelope.ProtocolVersion,
		DeviceSessionID: envelope.DeviceSessionID,
		MessageType:     envelope.MessageType,
		TimestampMS:     envelope.TimestampMS,
		RequestID:       envelope.RequestID,
		PayloadHash:     envelope.PayloadHash,
	}, envelope.SigningFields())
}

func TestResponseEnvelopeSigningFields(t *testing.T) {
	t.Parallel()

	envelope := types.ResponseEnvelope{
		ProtocolVersion: types.ProtocolVersionV1,
		RequestID:       "req-1",
		TimestampMS:     1_700_000_000_000,
		ResultCode:      types.ResultCodeOK,
		PayloadBytes:    []byte("payload"),
		PayloadHash:     []byte("01234567890123456789012345678901"),
		Signature:       []byte("ignored"),
	}

	assert.Equal(t, canon.ResponseSigningFields{
		ProtocolVersion: envelope.ProtocolVersion,
		RequestID:       envelope.RequestID,
		TimestampMS:     envelope.TimestampMS,
		ResultCode:      envelope.ResultCode,
		PayloadHash:     envelope.PayloadHash,
	}, envelope.SigningFields())
}

func TestEventEnvelopeSigningFieldsIncludesTraceID(t *testing.T) {
	t.Parallel()

	envelope := types.EventEnvelope{
		EventType:    "gateway.server_time",
		EventID:      "evt-1",
		TimestampMS:  1_700_000_000_000,
		RequestID:    "req-1",
		TraceID:      "trace-1",
		PayloadBytes: []byte("payload"),
		PayloadHash:  []byte("01234567890123456789012345678901"),
		Signature:    []byte("ignored"),
	}

	assert.Equal(t, canon.EventSigningFields{
		EventType:   envelope.EventType,
		EventID:     envelope.EventID,
		TimestampMS: envelope.TimestampMS,
		RequestID:   envelope.RequestID,
		TraceID:     envelope.TraceID,
		PayloadHash: envelope.PayloadHash,
	}, envelope.SigningFields())
}
