package services

import (
	"context"
	"github.com/cloudwego/eino/components/tool"
	"strings"
	"testing"
	"wa-assistant/backend/models"
)

func TestProductLookupFindsSeparatedNameWordsWithinAgent(t *testing.T) {
	db := agenticTestDB(t)
	if err := db.AutoMigrate(&models.Product{}); err != nil {
		t.Fatal(err)
	}
	for _, p := range []models.Product{
		{AgentID: 1, TenantID: 1, Name: "Umroh Reguler 9 Hari - Double (Demo)", Price: "Rp31.500.000"},
		{AgentID: 1, TenantID: 1, Name: "Umroh Reguler 9 Hari - Quad (Demo)", Price: "Rp28.500.000"},
		{AgentID: 2, TenantID: 2, Name: "Umroh Reguler Double Rahasia", Price: "Rp1"},
	} {
		if err := db.Create(&p).Error; err != nil {
			t.Fatal(err)
		}
	}
	state := &agentTurnState{}
	all, err := makeAgentTools(1, ChatOptions{DryRun: true}, state)
	if err != nil {
		t.Fatal(err)
	}
	var lookup tool.InvokableTool
	for _, candidate := range all {
		info, _ := candidate.Info(context.Background())
		if info.Name == "lookup_products" {
			lookup = candidate.(tool.InvokableTool)
		}
	}
	result, err := lookup.InvokableRun(context.Background(), `{"query":"Reguler Double"}`)
	if err != nil || !strings.Contains(result, "31.500.000") || strings.Contains(result, "Quad") || strings.Contains(result, "Rahasia") {
		t.Fatalf("result=%s err=%v", result, err)
	}
	result, err = lookup.InvokableRun(context.Background(), `{"query":"Reguler VIP"}`)
	if err != nil || strings.Contains(result, "31.500.000") {
		t.Fatalf("unmatched term was discarded: %s %v", result, err)
	}
}
