// +build integration

package routing

import (
	"context"
	"encoding/json"
	"os"
	"testing"
	"time"
)

// TestLLMRouterIntegration tests against real api.riotpiao.com
// Run with: go test -tags=integration -v -run TestLLMRouterIntegration
func TestLLMRouterIntegration(t *testing.T) {
	// Skip if not explicitly enabled
	if os.Getenv("RUN_INTEGRATION_TESTS") != "1" {
		t.Skip("Skipping integration test. Set RUN_INTEGRATION_TESTS=1 to run.")
	}

	kb, err := LoadKnowledgeBaseFromDefaultPath()
	if err != nil {
		t.Fatalf("failed to load knowledge base: %v", err)
	}

	router, err := NewLLMRouter(kb)
	if err != nil {
		t.Fatalf("failed to create router: %v", err)
	}

	tests := []struct {
		name     string
		input    LLMRouterInput
		validate func(*testing.T, *LLMRouterOutput)
	}{
		{
			name: "analyze repo request",
			input: LLMRouterInput{
				Message: "Analyze the GitHub repo https://github.com/rockliang/poimen for code quality and security issues",
				Context: map[string]interface{}{
					"branch": "main",
				},
			},
			validate: func(t *testing.T, output *LLMRouterOutput) {
				if output.IsCron {
					t.Error("expected one-time workflow, not cron")
				}
				if output.Spec == nil {
					t.Fatal("expected spec, got nil")
				}
				if len(output.Spec.States) < 2 {
					t.Errorf("expected at least 2 states, got %d", len(output.Spec.States))
				}
				// Should start with CloneRepoActivity
				if output.Spec.States[0].Resource != "CloneRepoActivity" {
					t.Errorf("expected first activity to be CloneRepoActivity, got %s", output.Spec.States[0].Resource)
				}
				t.Logf("Generated workflow: %s with %d states", output.Spec.Name, len(output.Spec.States))
				for i, state := range output.Spec.States {
					t.Logf("  State %d: %s (%s)", i, state.Name, state.Resource)
				}
			},
		},
		{
			name: "daily security scan (cron)",
			input: LLMRouterInput{
				Message: "Run a security scan on https://github.com/rockliang/poimen every day at 3 AM UTC",
			},
			validate: func(t *testing.T, output *LLMRouterOutput) {
				if !output.IsCron {
					t.Error("expected cron workflow")
				}
				if output.CronSpec == nil {
					t.Fatal("expected cron spec, got nil")
				}
				if output.CronSpec.Schedule == "" {
					t.Error("expected cron schedule")
				}
				t.Logf("Generated cron workflow: %s, schedule: %s", output.CronSpec.Name, output.CronSpec.Schedule)
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
			defer cancel()

			output, err := router.Route(ctx, tt.input)
			if err != nil {
				t.Fatalf("Route failed: %v", err)
			}

			// Pretty print output
			jsonOut, _ := json.MarshalIndent(output, "", "  ")
			t.Logf("Output:\n%s", string(jsonOut))

			if tt.validate != nil {
				tt.validate(t, output)
			}
		})
	}
}
