package utils_ollama

import (
	"ai_scheduler/internal/config"
	"context"

	"encoding/json"
	"testing"

	"gitea.cdlsxd.cn/self-tools/l_request"
	"github.com/ollama/ollama/api"
)

func TestToolSelectApi(t *testing.T) {
	//ctx := context.Background()
	//client, clean, err := NewClient(&config.Config{
	//	Ollama: config.OllamaConfig{
	//		BaseURL:       "http://127.0.0.1:11434",
	//		Model:         "qwen3-coder:480b-cloud",
	//		GenerateModel: "qwen3-coder:480b-cloud",
	//	},
	//})
	//if err != nil {
	//	panic(err)
	//}
	//defer clean()
	prompt := []m{
		{
			Role:    "user",
			Content: "",
			Images:  getImgDataLocalTest("1.jpg"),
		},
	}
	red := q{
		Model:    "qwen3-coder:480b-cloud",
		Messages: prompt,
		Stream:   false,
	}
	d, _ := json.Marshal(red)
	//res, err := client.ToolSelect(ctx, prompt, nil)
	req := l_request.Request{
		Method:   "POST",
		JsonByte: d,
		Url:      "http://localhost:11434/api/chat",
	}
	res, err := req.Send()
	t.Log(res, err)
}

type q struct {
	Messages []m    `json:"messages"`
	Model    string `json:"model"`
	Stream   bool   `json:"stream"`
}

type m struct {
	Content string   `json:"content"`
	Images  []string `json:"images"`
	Role    string   `json:"role"`
}

func TestToolSelectFunc(t *testing.T) {
	ctx := context.Background()
	client, clean, err := NewClient(&config.Config{
		Ollama: config.OllamaConfig{
			BaseURL:       "http://127.0.0.1:11434",
			Model:         "qwen2.5vl:3b",
			GenerateModel: "qwen2.5vl:3b",
		},
	})
	if err != nil {
		panic(err)
	}
	defer clean()
	imageURL := "https://k.sinaimg.cn/n/spider20240725/54/w1280h2774/20240725/93de-4676dc72948b4363893e08b55df872ec.jpg/w700d1q75cms.jpg"
	req := l_request.Request{
		Method: "GET",
		Url:    imageURL,
	}
	res, err := req.Send()
	if err != nil {
		t.Fatal(err)
	}

	var prompt = make([]api.Message, 0)
	prompt = append(prompt, api.Message{
		Role:    "user",
		Content: "详细描述图片内容",
		Images: []api.ImageData{
			res.Content,
		},
	})

	chat, err := client.ToolSelect(ctx, prompt, nil)

	t.Log(chat, err)
}

func getImgDataLocalTest(imgUrl string) (img []string) {
	//image, err := os.ReadFile(imgUrl)
	//if err != nil {
	//	panic(err)
	//}
	img = []string{}
	//encode := base64.StdEncoding.EncodeToString(image)
	img = append(img, base)

	return
}

const base = `iVBORw0KGgoAAAANSUhEUgAAANIAAAB4CAYAAACHHqzKAAAAAXNSR0IArs4c6QAAAIRlWElmTU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEb
[encoded data, 59774 characters]
voQTefWxZ9CMIP9/0Wj1ClQSO/4AAAAASUVORK5CYII=`
