package experiments

import (
	"fmt"
	"log"
	"testing"

	"gitlab.alibaba-inc.com/pai_biz_arch/pairec-experiment/client_golang/common"
	"gitlab.alibaba-inc.com/pai_biz_arch/pairec-experiment/client_golang/model"
)

func TestCreateExperimentClient(t *testing.T) {
	host := "http://localhost:8080"
	client, err := NewExperimentClient(host, common.Environment_Prepub_Desc, WithLogger(LoggerFunc(log.Printf)))
	if err != nil {
		t.Error(err)
	}
	fmt.Println(client)
}
func TestLoadExperimentData(t *testing.T) {
	host := "http://localhost:8080"
	_, err := NewExperimentClient(host, common.Environment_Prepub_Desc, WithLogger(LoggerFunc(log.Printf)))
	if err != nil {
		t.Error(err)
	}

	// client.LoadExperimentData()
}

func TestMatchExperiment(t *testing.T) {
	host := "http://localhost:8080"
	client, err := NewExperimentClient(host, common.Environment_Prepub_Desc, WithLogger(LoggerFunc(log.Printf)), WithErrorLogger(LoggerFunc(log.Fatalf)))
	if err != nil {
		t.Fatal(err)
	}

	experimentContext := model.ExperimentContext{
		RequestId: "pvid",
		Uid:       "2115",
		FilterParams: map[string]interface{}{
			"sex": "male",
			//"age": 35,
			"uid": 1,
		},
	}

	experimentResult := client.MatchExperiment("home_feed", &experimentContext)

	fmt.Println(experimentResult.Info())
	fmt.Println(experimentResult.GetExpId())

	fmt.Println(experimentResult.GetLayerParams("").GetString("a", "not exist"))
	fmt.Println(experimentResult.GetExperimentParams().GetString("a", "not exist"))

}

func TestMatchExperiment2(t *testing.T) {
	host := "http://1730760139076263.cn-beijing.pai-eas.aliyuncs.com/api/predict/pairec_experiment"
	client, err := NewExperimentClient(host, common.Environment_Prepub_Desc, WithLogger(LoggerFunc(log.Printf)), WithErrorLogger(LoggerFunc(log.Fatalf)), WithToken("YjQ2MTczYThlMGE3MzQ0NzIyNDE5OTQ2N2I0NmE2MWUzZjRkM2UzNg=="))
	if err != nil {
		t.Fatal(err)
	}

	experimentContext := model.ExperimentContext{
		RequestId: "pvid",
		//Uid:       "211789768",
		Uid: "102441835",
		FilterParams: map[string]interface{}{
			"sex": "male",
			//"age": 35,
			"uid": 1,
		},
	}

	experimentResult := client.MatchExperiment("homepage", &experimentContext)

	fmt.Println(experimentResult.Info())
	fmt.Println(experimentResult.GetExpId())

	fmt.Println(experimentResult.GetExperimentParams().GetInt("ivalue", 0))
	fmt.Println(experimentResult.GetExperimentParams().GetString("svalue", "not exist"))
	fmt.Println(experimentResult.GetExperimentParams().GetFloat("dvalue", 0))

}
func TestGetSceneParam(t *testing.T) {
	host := "http://localhost:8000"
	client, err := NewExperimentClient(host, common.Environment_Prepub_Desc, WithLogger(LoggerFunc(log.Printf)), WithErrorLogger(LoggerFunc(log.Fatalf)))
	if err != nil {
		t.Fatal(err)
	}

	param := client.GetSceneParams("home_feed").GetString("_feature_consistency_job_", "not exist")
	fmt.Println(param)
}
func TestGetFeatureConsistencyJob(t *testing.T) {
	host := "http://localhost:8000"
	client, err := NewExperimentClient(host, common.Environment_Prepub_Desc, WithLogger(LoggerFunc(log.Printf)), WithErrorLogger(LoggerFunc(log.Fatalf)))
	if err != nil {
		t.Fatal(err)
	}

	jobs := client.GetSceneParams("home_feed").GetFeatureConsistencyJobs()
	for _, job := range jobs {
		fmt.Println(job)
	}
}
