// Command generate_business_notification_golden prints the cross-language
// fixtures consumed by the C++ protocol test. It intentionally uses the same
// Go encoding/json primitives as the server canonicalizer.
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"strings"
)

type envelope struct {
	SchemaVersion uint32          `json:"schema_version"`
	Visibility    string          `json:"visibility"`
	Encrypted     bool            `json:"encrypted"`
	Title         string          `json:"title"`
	Body          string          `json:"body"`
	Data          json.RawMessage `json:"data,omitempty"`
}

func canonicalObject(input string) json.RawMessage {
	decoder := json.NewDecoder(bytes.NewBufferString(input))
	decoder.UseNumber()
	var value map[string]any
	if err := decoder.Decode(&value); err != nil {
		panic(err)
	}
	result, err := json.Marshal(value)
	if err != nil {
		panic(err)
	}
	return result
}

func emit(name, title, body string, data json.RawMessage) {
	payload, err := json.Marshal(envelope{
		SchemaVersion: 1,
		Visibility:    "server_visible",
		Encrypted:     false,
		Title:         strings.TrimSpace(title),
		Body:          body,
		Data:          data,
	})
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s\t%s\n", name, payload)
}

func main() {
	emit("without_data", "Maintenance", "Tonight at 23:00", nil)
	emit(
		"go_encoding_json_edges",
		"A<\u2028&\u2029>B",
		"\u524d\u5bfc\u7a7a\u683c \U0001f600 ",
		canonicalObject(`{"nested":{"z":null,"a":true},"line":"\u2028","html":"<&>","huge":123456789012345678901234567890,"exp":1E+09,"negative_zero":-0}`),
	)
	emit("empty_data_object", "Empty data", "Object is present", json.RawMessage(`{}`))
}
