package background

import (
	"testing"
)

func TestExtractProactiveMessage_NoMarker(t *testing.T) {
	thinker := &Thinker{platformFormats: defaultPlatformFormats()}
	msg, target := thinker.extractProactiveMessage("今天天气真好，开拓者应该出门走走。")
	if msg != "" {
		t.Errorf("expected empty, got %q", msg)
	}
	if target != nil {
		t.Errorf("expected nil target, got %+v", target)
	}
}

func TestExtractProactiveMessage_WebPush(t *testing.T) {
	thinker := &Thinker{platformFormats: defaultPlatformFormats()}
	content := `今天在想开拓者的事…
【主动消息】今天天气真好，要出去走走吗♪`
	msg, target := thinker.extractProactiveMessage(content)
	if msg == "" {
		t.Fatal("expected message, got empty")
	}
	if target != nil {
		t.Errorf("expected nil target (web push), got %+v", target)
	}
	if msg != "今天天气真好，要出去走走吗♪" {
		t.Errorf("unexpected message: %q", msg)
	}
}

func TestExtractProactiveMessage_OBv11Group(t *testing.T) {
	thinker := &Thinker{platformFormats: defaultPlatformFormats()}
	content := `反思中…
【主动消息】【OBv11群聊:123456】大家早上好呀♪`
	msg, target := thinker.extractProactiveMessage(content)
	if msg == "" {
		t.Fatal("expected message, got empty")
	}
	if target == nil {
		t.Fatal("expected target, got nil")
	}
	if target.Platform != "obv11" {
		t.Errorf("expected platform 'obv11', got %q", target.Platform)
	}
	if target.ChatType != "group" {
		t.Errorf("expected chatType 'group', got %q", target.ChatType)
	}
	if target.GroupID != "123456" {
		t.Errorf("expected groupID '123456', got %q", target.GroupID)
	}
	if msg != "大家早上好呀♪" {
		t.Errorf("unexpected message: %q", msg)
	}
}

func TestExtractProactiveMessage_OBv11Private(t *testing.T) {
	thinker := &Thinker{platformFormats: defaultPlatformFormats()}
	content := `【主动消息】【OBv11私聊:789012】好久不见，最近怎么样？`
	msg, target := thinker.extractProactiveMessage(content)
	if msg == "" {
		t.Fatal("expected message, got empty")
	}
	if target == nil {
		t.Fatal("expected target, got nil")
	}
	if target.ChatType != "private" {
		t.Errorf("expected chatType 'private', got %q", target.ChatType)
	}
	if target.UserID != "789012" {
		t.Errorf("expected userID '789012', got %q", target.UserID)
	}
	if target.GroupID != "" {
		t.Errorf("expected empty groupID for private chat, got %q", target.GroupID)
	}
	if msg != "好久不见，最近怎么样？" {
		t.Errorf("unexpected message: %q", msg)
	}
}

func TestExtractProactiveMessage_OBv11GroupAt(t *testing.T) {
	thinker := &Thinker{platformFormats: defaultPlatformFormats()}
	content := `【主动消息】【OBv11群聊:123456@999888】你说得对呢`
	msg, target := thinker.extractProactiveMessage(content)
	if msg == "" {
		t.Fatal("expected message, got empty")
	}
	if target == nil {
		t.Fatal("expected target, got nil")
	}
	if target.AtUserID != "999888" {
		t.Errorf("expected atUserID '999888', got %q", target.AtUserID)
	}
}

func TestExtractProactiveMessage_Negation(t *testing.T) {
	thinker := &Thinker{platformFormats: defaultPlatformFormats()}
	// "不需要" negates the marker.
	content := "开拓者在休息，不需要写【主动消息】打扰他。"
	msg, _ := thinker.extractProactiveMessage(content)
	if msg != "" {
		t.Errorf("expected empty (negated), got %q", msg)
	}
}

func TestExtractProactiveMessage_ContinueMarker(t *testing.T) {
	thinker := &Thinker{platformFormats: defaultPlatformFormats()}
	// 【继续思考】should NOT be extracted as proactive message.
	content := "反思完成。【继续思考】"
	msg, target := thinker.extractProactiveMessage(content)
	if msg != "" {
		t.Errorf("expected empty, got %q", msg)
	}
	if target != nil {
		t.Errorf("expected nil target, got %+v", target)
	}
}
