package fullservernotice

import (
	"strings"
	"testing"
)

func TestBuildFanoutJSONForUserIDScope(t *testing.T) {
	req, err := normalizeFanoutRequest(fanoutRequest{
		MessageType:  "activity",
		TargetScope:  "user_ids",
		UserIDs:      []int64{42, 43, 42, 0},
		Title:        "活动上线",
		Summary:      "新活动已上线。",
		ActionType:   "activity_detail",
		ActionParam:  "activity_id=act_01",
		MetadataJSON: `{"source":"admin"}`,
		BatchSize:    500,
	}, 1)
	if err != nil {
		t.Fatalf("normalizeFanoutRequest failed: %v", err)
	}
	targetFilterJSON, err := buildTargetFilterJSON(req)
	if err != nil {
		t.Fatalf("buildTargetFilterJSON failed: %v", err)
	}
	if targetFilterJSON != `{"user_ids":[42,43]}` {
		t.Fatalf("target filter mismatch: %s", targetFilterJSON)
	}
	templateSnapshotJSON, err := buildTemplateSnapshotJSON(req)
	if err != nil {
		t.Fatalf("buildTemplateSnapshotJSON failed: %v", err)
	}
	for _, required := range []string{`"title":"活动上线"`, `"action_type":"activity_detail"`, `"metadata_json":{"source":"admin"}`} {
		if !strings.Contains(templateSnapshotJSON, required) {
			t.Fatalf("template snapshot %s missing %s", templateSnapshotJSON, required)
		}
	}
}

func TestNormalizeFanoutRequestRequiresTargetParameters(t *testing.T) {
	req, err := normalizeFanoutRequest(fanoutRequest{
		MessageType: "system",
		TargetScope: "country",
		Title:       "维护通知",
		Summary:     "今晚维护。",
		BatchSize:   500,
	}, 1)
	if err != nil {
		t.Fatalf("normalizeFanoutRequest failed: %v", err)
	}
	if _, err := buildTargetFilterJSON(req); err == nil {
		t.Fatalf("country target without country should fail")
	}
}
