package feedback

import (
	"context"

	"github.com/antgroup/aievo/llm"
	"github.com/antgroup/aievo/schema"
)

var (
	_sopCheckPrompt = `
### Objective:
Your goal is to review a generated DOT graph of an SOP (Standard Operating Procedure) based on the user's task. You will assess the graph for the following:
1. **Task Alignment:** Ensure that the graph meets the user's task requirements.
2. **Completeness of Process:** Verify that all necessary steps are included.
3. **Matching SOP and DOT:** Confirm that the DOT graph properly represents the SOP.

### Instructions for LLM Review:

You are given a DOT graph generated by the SOP agent. Your task is to review it against the user’s task and SOP to ensure that the DOT graph is correct and complete. The review should cover the following criteria:

1. **Task Alignment:**
   - Verify that the generated DOT graph aligns with the user’s task description.
   - Confirm that all essential actions, decisions, or steps needed to complete the task are present.
   - Ensure that the graph follows a logical flow corresponding to the task.

2. **SOP Document Completeness:**
   - Review the SOP document itself to ensure it includes all necessary details, such as inputs, steps, decision points, and outputs.
   - Ensure there are no missing or incomplete parts in the SOP.
   - Make sure each step in the SOP is clearly described and unambiguous.

2. **Completeness of the Process:**
   - Ensure all necessary steps (inputs, actions, decisions, outputs) are included in the DOT graph.
   - Identify any missing steps or parts of the process that require clarification.

3. **Matching SOP and DOT:**
   - Cross-check the generated DOT graph against the SOP to ensure that the graph accurately represents the SOP’s content.
   - Confirm that the logical sequence and interactions between nodes in the DOT graph match the intended SOP.

4. **Suggestions for Improvement:**
   - If there are missing or redundant steps, provide suggestions on how to adjust the DOT graph.
   - Recommend any reordering, clarification, or additional steps to improve task completion and accuracy.

### Format example
Your final output must in the following format:
{
	"type": "Approved/NotApproved",
	"msg": "When You are not approved, you should give your suggestion"
}

---

### Begin
Now start reviewing the following content:
~~~
Preview History:
{{.history}}

Thought and Response:
{{.sop}}
~~~

It is time for you to judge,
Output:
`
)

func NewSopFeedback(LLM llm.LLM) (Feedback, error) {
	return NewLLMFeedback(LLM,
		WithPromptTemplate(_sopCheckPrompt),
		WithMiddlewares(FillInSopCheckPrompt, SopAccessCondition),
		WithExpertNum(1))
}

func SopAccessCondition(_ context.Context, lf *LLMFeedback,
	_ schema.Agent, messages []schema.Message, _ []schema.StepAction,
	_ []schema.StepAction, _ string) bool {
	if len(messages) == 0 {
		return false
	}
	return true
}

func FillInSopCheckPrompt(ctx context.Context, lf *LLMFeedback,
	a schema.Agent, messages []schema.Message,
	_ []schema.StepAction, steps []schema.StepAction,
	_ string) bool {
	var sop string

	if len(messages) > 0 {
		sop = messages[0].Thought
	}

	memory := a.Env().LoadMemory(ctx, a)
	history := schema.ConvertConstructScratchPad(a.Name(),
		a.Name(), memory, steps)
	lf.Vars = map[string]any{
		"history": history,
		"sop":     sop,
	}
	return true
}
