package orchestrator

import (
	"testing"
)

func TestIsSimpleGreeting(t *testing.T) {
	a := &IntentAnalyzer{}

	tests := []struct {
		name     string
		input    string
		expected bool
	}{
		// Exact matches
		{"你好 (exact)", "你好", true},
		{"hello (exact)", "hello", true},
		{"早上好 (exact)", "早上好", true},
		{"晚安 (exact)", "晚安", true},
		{"谢谢 (exact)", "谢谢", true},
		{"在吗 (exact)", "在吗", true},
		{"再见 (exact)", "再见", true},
		{"单个嗯", "嗯", true},

		// Short messages (<=4 chars, no complex keywords)
		{"极短消息", "好的呀", true},
		{"短闲聊", "哈哈", true},
		{"OK", "ok", true},

		// Short but with IoT/task keywords → not a greeting
		{"短IoT关键词", "开灯", false},
		{"短问题", "怎么", false},
		{"短设备", "灯", false},
		{"帮我", "帮我", false},

		// Longer messages → not a greeting
		{"正常对话", "今天天气真好呀", false},
		{"长问候", "昔涟早上好呀，今天怎么样", false},
		{"带问题", "你好，帮我开灯好吗", false},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := a.isSimpleGreeting(tt.input)
			if got != tt.expected {
				t.Errorf("isSimpleGreeting(%q) = %v, want %v", tt.input, got, tt.expected)
			}
		})
	}
}

func TestIsStrongIoTCommand(t *testing.T) {
	a := &IntentAnalyzer{}

	tests := []struct {
		name     string
		input    string
		expected bool
	}{
		// Control + device combinations → true
		{"打开灯", "打开客厅灯", true},
		{"关掉空调", "关掉卧室空调", true},
		{"打开电视", "打开电视", true},
		{"关闭窗帘", "关闭窗帘", true},
		{"调到26度", "把空调调到26度", true},
		{"设置温度", "设置空调温度", true},
		{"关掉风扇", "关掉风扇", true},

		// No device word → false
		{"仅控制词", "打开", false},
		{"仅设备词", "灯开了吗", false},
		{"仅查询", "现在客厅灯是什么状态", false},

		// Neither → false
		{"普通对话", "你好呀", false},
		{"闲聊", "今天天气不错", false},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := a.isStrongIoTCommand(tt.input)
			if got != tt.expected {
				t.Errorf("isStrongIoTCommand(%q) = %v, want %v", tt.input, got, tt.expected)
			}
		})
	}
}

func TestKeywordAnalyze(t *testing.T) {
	a := &IntentAnalyzer{}

	tests := []struct {
		name             string
		input            string
		wantPrimary      string
		wantNeedsIoT     bool
		wantSentiment    string
	}{
		{"IoT命令", "打开客厅灯", "iot_control", true, "neutral"},
		{"IoT查询", "现在灯是什么状态", "question", true, "neutral"},
		{"I情感正面", "今天好开心呀", "chat", false, "positive"},
		{"I情感负面", "我今天好累", "emotional", false, "negative"},
		{"I提问", "怎么学习日语", "question", false, "neutral"},
		{"I普通聊天", "今天天气真好", "chat", false, "neutral"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := a.keywordAnalyze(tt.input)
			if got.Primary != tt.wantPrimary {
				t.Errorf("keywordAnalyze(%q).Primary = %q, want %q", tt.input, got.Primary, tt.wantPrimary)
			}
			if got.NeedsIoT != tt.wantNeedsIoT {
				t.Errorf("keywordAnalyze(%q).NeedsIoT = %v, want %v", tt.input, got.NeedsIoT, tt.wantNeedsIoT)
			}
			if got.Sentiment != tt.wantSentiment {
				t.Errorf("keywordAnalyze(%q).Sentiment = %q, want %q", tt.input, got.Sentiment, tt.wantSentiment)
			}
		})
	}
}

func TestParseIntentResponse(t *testing.T) {
	tests := []struct {
		name    string
		input   string
		want    string // expected Primary
		wantErr bool
	}{
		{"纯净JSON", `{"primary":"chat","needs_iot":false,"needs_memory":true,"sentiment":"positive","urgency":"low"}`, "chat", false},
		{"Markdown包裹", "```json\n{\"primary\":\"iot_control\",\"needs_iot\":true,\"needs_memory\":true,\"sentiment\":\"neutral\",\"urgency\":\"high\"}\n```", "iot_control", false},
		{"前后有空白", "  \n{\"primary\":\"question\",\"needs_iot\":false,\"needs_memory\":true,\"sentiment\":\"neutral\",\"urgency\":\"medium\"}\n  ", "question", false},
		{"JSON前后有文字", "分析结果：{\"primary\":\"chat\",\"needs_iot\":false,\"needs_memory\":true,\"sentiment\":\"neutral\",\"urgency\":\"low\"}，仅供参考", "chat", false},
		{"默认值填充", `{"needs_iot":true}`, "chat", false}, // Primary 默认为 "chat"
		{"无效JSON", "不是JSON", "", true},
		{"空字符串", "", "", true},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := parseIntentResponse(tt.input)
			if tt.wantErr {
				if err == nil {
					t.Errorf("parseIntentResponse(%q) expected error, got nil", tt.input)
				}
				return
			}
			if err != nil {
				t.Errorf("parseIntentResponse(%q) unexpected error: %v", tt.input, err)
				return
			}
			if got.Primary != tt.want {
				t.Errorf("parseIntentResponse(%q).Primary = %q, want %q", tt.input, got.Primary, tt.want)
			}
		})
	}
}
