// TypeScript port of the canonical response-signing serializer in
// `ui/core/canon/response.go` (`BuildResponseSigningInput`). Used by
// the Phase 7 Playwright spec to forge gateway responses and sign
// them with the fixture key. The Go-side parity check
// (`gateway/authn/parity_with_ui_core_test.go`) is the source of
// truth; this TS copy stays small enough to read against that test.

const RESPONSE_DOMAIN_MARKER_V1 = "galaxy-response-v1";
const EVENT_DOMAIN_MARKER_V1 = "galaxy-event-v1";

export interface ResponseSigningFields {
	protocolVersion: string;
	requestId: string;
	timestampMs: bigint;
	resultCode: string;
	payloadHash: Uint8Array;
}

export interface EventSigningFields {
	eventType: string;
	eventId: string;
	timestampMs: bigint;
	requestId: string;
	traceId: string;
	payloadHash: Uint8Array;
}

export function buildResponseSigningInput(
	fields: ResponseSigningFields,
): Uint8Array {
	const parts: number[] = [];
	appendLengthPrefixedString(parts, RESPONSE_DOMAIN_MARKER_V1);
	appendLengthPrefixedString(parts, fields.protocolVersion);
	appendLengthPrefixedString(parts, fields.requestId);
	appendBigEndianUint64(parts, fields.timestampMs);
	appendLengthPrefixedString(parts, fields.resultCode);
	appendLengthPrefixedBytes(parts, fields.payloadHash);
	return new Uint8Array(parts);
}

// `buildEventSigningInput` mirrors `ui/core/canon/event.go`
// `BuildEventSigningInput`. The Go-side parity test
// (`gateway/authn/parity_with_ui_core_test.go`) is the source of truth;
// this TS copy stays close enough to that test to read against it.
export function buildEventSigningInput(
	fields: EventSigningFields,
): Uint8Array {
	const parts: number[] = [];
	appendLengthPrefixedString(parts, EVENT_DOMAIN_MARKER_V1);
	appendLengthPrefixedString(parts, fields.eventType);
	appendLengthPrefixedString(parts, fields.eventId);
	appendBigEndianUint64(parts, fields.timestampMs);
	appendLengthPrefixedString(parts, fields.requestId);
	appendLengthPrefixedString(parts, fields.traceId);
	appendLengthPrefixedBytes(parts, fields.payloadHash);
	return new Uint8Array(parts);
}

function appendLengthPrefixedString(dst: number[], value: string): void {
	const bytes = new TextEncoder().encode(value);
	appendLengthPrefixedBytes(dst, bytes);
}

function appendLengthPrefixedBytes(dst: number[], value: Uint8Array): void {
	appendUvarint(dst, BigInt(value.length));
	for (let i = 0; i < value.length; i++) {
		dst.push(value[i]!);
	}
}

function appendUvarint(dst: number[], value: bigint): void {
	let v = value;
	while (v >= 0x80n) {
		dst.push(Number(v & 0xffn) | 0x80);
		v >>= 7n;
	}
	dst.push(Number(v & 0xffn));
}

function appendBigEndianUint64(dst: number[], value: bigint): void {
	const v = value & 0xffffffffffffffffn;
	for (let i = 7; i >= 0; i--) {
		dst.push(Number((v >> BigInt(i * 8)) & 0xffn));
	}
}
