package adk

import (
	"context"
	"encoding/json"
	"testing"

	"github.com/orchard9/stemedb-go/steme"
)

func TestSupersedeTool(t *testing.T) {
	// Track what supersession was created
	var capturedParams steme.SupersedeParams

	client := &mockClient{
		supersedeFunc: func(ctx context.Context, params steme.SupersedeParams) (*steme.SupersedeResult, error) {
			capturedParams = params
			return &steme.SupersedeResult{
				Status:           "superseded",
				TargetHash:       params.TargetHash,
				SupersessionType: params.SupersessionType,
				Timestamp:        1704067200,
			}, nil
		},
	}

	tool := NewSupersedeTool(client)

	input := SupersedeInput{
		Hash:    "abc123def456",
		Type:    "Invalidate",
		Reason:  "Proposal treated as approved. See incident INC-2024-001",
		NewHash: "def456abc123",
		AgentID: "deadbeef00000000000000000000000000000000000000000000000000000000",
		Signature: "0000000000000000000000000000000000000000000000000000000000000000" +
			"0000000000000000000000000000000000000000000000000000000000000000",
	}

	inputBytes, err := json.Marshal(input)
	if err != nil {
		t.Fatalf("failed to marshal input: %v", err)
	}

	outputBytes, err := tool.Execute(context.Background(), inputBytes)
	if err != nil {
		t.Fatalf("supersede failed: %v", err)
	}

	var output SupersedeOutput
	if err := json.Unmarshal(outputBytes, &output); err != nil {
		t.Fatalf("failed to unmarshal output: %v", err)
	}

	// Verify output
	if !output.Success {
		t.Errorf("expected success, got error: %s", output.Error)
	}

	if output.TargetHash != "abc123def456" {
		t.Errorf("expected target hash abc123def456, got %s", output.TargetHash)
	}

	if output.SupersessionType != "Invalidate" {
		t.Errorf("expected supersession type Invalidate, got %s", output.SupersessionType)
	}

	// Verify captured params
	if capturedParams.TargetHash != "abc123def456" {
		t.Errorf("expected target hash abc123def456, got %s", capturedParams.TargetHash)
	}

	if capturedParams.SupersessionType != steme.SupersessionInvalidate {
		t.Errorf("expected supersession type Invalidate, got %s", capturedParams.SupersessionType)
	}

	if capturedParams.Reason != "Proposal treated as approved. See incident INC-2024-001" {
		t.Errorf("expected reason about proposal, got %s", capturedParams.Reason)
	}
}

func TestParseSupersessionType(t *testing.T) {
	tests := []struct {
		input    string
		expected steme.SupersessionType
	}{
		{"Invalidate", steme.SupersessionInvalidate},
		{"invalidate", steme.SupersessionInvalidate},
		{"Temporal", steme.SupersessionTemporal},
		{"temporal", steme.SupersessionTemporal},
		{"Refinement", steme.SupersessionRefinement},
		{"RequiresReview", steme.SupersessionRequiresReview},
		{"requires_review", steme.SupersessionRequiresReview},
		{"Additive", steme.SupersessionAdditive},
		{"additive", steme.SupersessionAdditive},
		{"unknown", steme.SupersessionInvalidate}, // Default
	}

	for _, tt := range tests {
		result := parseSupersessionType(tt.input)
		if result != tt.expected {
			t.Errorf("parseSupersessionType(%s) = %v, expected %v", tt.input, result, tt.expected)
		}
	}
}
