package action

import (
	"context"
	"fmt"

	"github.com/rockliang/poimen/workflows/action/llm"
	"github.com/rockliang/poimen/workflows/prompts"
	"github.com/rockliang/poimen/workflows/statemachine"
)

// JudgeInput is input to JudgeActivity.
type JudgeInput struct {
	Config              statemachine.OrchestratorConfig
	Diff                string // git diff output
	IntegrationTestLogs string // test output
}

// JudgeOutput is the output of JudgeActivity.
type JudgeOutput struct {
	Verdict  string // "pass" or "fail"
	Critique string // explanation if fail
}

// JudgeActivity calls the Judge LLM to review correctness.
func JudgeActivity(ctx context.Context, in JudgeInput) (JudgeOutput, error) {
	// Get LLM client
	client, err := llm.NewClient()
	if err != nil {
		return JudgeOutput{}, fmt.Errorf("failed to create LLM client: %w", err)
	}

	// Get judge spec
	judgeSpec, exists := in.Config.RolePrompts["judge"]
	if !exists {
		return JudgeOutput{}, fmt.Errorf("judge role prompt not configured")
	}

	// Render template
	var templateContent string
	if judgeSpec.RawTemplate != "" {
		templateContent = judgeSpec.RawTemplate
	} else {
		// Parse and render the embedded template
		templateContent, err = prompts.Render(judgeSpec.TemplateRef, map[string]any{
			"SystemPrompt": in.Config.SystemPrompt,
			"Diff":         in.Diff,
			"TestResult":   in.IntegrationTestLogs,
		})
		if err != nil {
			return JudgeOutput{}, fmt.Errorf("failed to render judge template: %w", err)
		}
	}

	// Call LLM
	messages := []llm.MessageParam{
		{
			Role:    "user",
			Content: templateContent,
		},
	}

	response, err := client.CreateMessage(ctx, llm.MessageInput{
		Model:        judgeSpec.Model,
		SystemPrompt: in.Config.SystemPrompt,
		Messages:     messages,
	})
	if err != nil {
		return JudgeOutput{}, fmt.Errorf("judge LLM call failed: %w", err)
	}

	// For now, return a default pass verdict
	// In full implementation, would parse LLM response
	return JudgeOutput{
		Verdict:  "pass",
		Critique: response,
	}, nil
}
