package workflow

import (
	"agent/src/message"
	"agent/src/prompt"
	"agent/src/utils/log"
)



func (task *Task) explain() error {
	req := task.request.(*message.RequestExecExplain)
	// selectText, err := json.Marshal(req.SelectedText)
	// if err != nil {
	// 	return err
	// }
	// visibleText, err := json.Marshal(req.VisibleText)
	// if err != nil {
	// 	return err
	// }
	// msg := prompt.MakeExecExplain(req.Language, string(selectText), string(visibleText))

	msg := prompt.MakeExecExplain(req.Language, req.SelectedText.Text, req.VisibleText.Text)
	return task.chat(msg, req.Stream)
}

func (task *Task) docstring() error {
	log.Info("call docstring")
	req := task.request.(*message.RequestExecDocstring)
	contents := prompt.MakeExecDocstring(req.Language, req.SelectedText.FilePath, req.SelectedText.Text)

	answer, err := task.pool.llmGroup.ChatContents(task.ctx, contents)
	if err != nil {
		return err
	}
	answer = prompt.RemoveThinkPart(answer)

	// TODO extract doc part

	return task.client.WriteJson(&message.ResponseExecSuccess{
		RequestId: task.request_id,
		Msg      : answer,
	}, false)
}

func (task *Task) fix() error {
	req := task.request.(*message.RequestExecFix)
	msg := prompt.MakeExecFix(req.Language, req.SelectedText.Text)
	return task.chat(msg, req.Stream)
}


func (task *Task) optimize() error {
	req := task.request.(*message.RequestExecFix)
	msg := prompt.MakeExecOptimize(req.Language, req.SelectedText.Text)
	return task.chat(msg, req.Stream)
}

