package parser

import (
	"testing"
)

func TestParseAgents_BasicPolicies(t *testing.T) {
	content := `# Policies
- Don't use hard-coded color values
- Always use theme colors
`
	policies, err := ParseAgents(content)
	if err != nil {
		t.Fatalf("ParseAgents failed: %v", err)
	}

	if len(policies) != 2 {
		t.Fatalf("Expected 2 policies, got %d", len(policies))
	}

	// Check first policy was extracted
	firstPolicy := policies[0]
	if firstPolicy.Message == "" {
		t.Error("Expected policy message to be non-empty")
	}
	if firstPolicy.OriginalText == "" {
		t.Error("Expected original text to be non-empty")
	}
}

func TestParseAgents_ExtractsMessages(t *testing.T) {
	content := `# Rules
- Don't use eval
- Don't use document.write
`
	policies, err := ParseAgents(content)
	if err != nil {
		t.Fatalf("ParseAgents failed: %v", err)
	}

	if len(policies) != 2 {
		t.Fatalf("Expected 2 policies, got %d", len(policies))
	}

	// Verify policies have content
	for i, policy := range policies {
		if policy.Message == "" {
			t.Errorf("Policy %d has empty message", i)
		}
		if policy.Name == "" {
			t.Errorf("Policy %d has empty name", i)
		}
	}
}

func TestParseAgents_EmptyContent(t *testing.T) {
	content := ``
	policies, err := ParseAgents(content)
	if err != nil {
		t.Fatalf("ParseAgents failed: %v", err)
	}

	if len(policies) != 0 {
		t.Fatalf("Expected 0 policies, got %d", len(policies))
	}
}

func TestParseAgents_BulletPoints(t *testing.T) {
	content := `# Policies
* Don't use hardcoded colors
- Don't use eval
1. Don't use document.write
`
	policies, err := ParseAgents(content)
	if err != nil {
		t.Fatalf("ParseAgents failed: %v", err)
	}

	if len(policies) != 3 {
		t.Fatalf("Expected 3 policies, got %d", len(policies))
	}

	// All policies should have content
	for _, policy := range policies {
		if policy.Message == "" || policy.Name == "" {
			t.Errorf("Policy missing required fields: %+v", policy)
		}
	}
}

func TestParseAgents_SkipsHeaders(t *testing.T) {
	content := `# Main Policies
## Subsection
### Details
- Don't use eval
`
	policies, err := ParseAgents(content)
	if err != nil {
		t.Fatalf("ParseAgents failed: %v", err)
	}

	if len(policies) != 1 {
		t.Fatalf("Expected 1 policy, got %d", len(policies))
	}
}

func TestParseAgents_PreservesOriginalText(t *testing.T) {
	content := `# Python Policies
- Use ruff format instead of black
`
	policies, err := ParseAgents(content)
	if err != nil {
		t.Fatalf("ParseAgents failed: %v", err)
	}

	if len(policies) != 1 {
		t.Fatalf("Expected 1 policy, got %d", len(policies))
	}

	policy := policies[0]
	if policy.OriginalText == "" {
		t.Error("Expected OriginalText to be preserved")
	}
	if policy.Message == "" {
		t.Error("Expected Message to be set")
	}
	if policy.Name == "" {
		t.Error("Expected Name to be set")
	}
}

func TestParseAgents_MultilinePolicies(t *testing.T) {
	content := `# Database Policies
- Always use async when database operations
- Use connection pooling for better performance
`
	policies, err := ParseAgents(content)
	if err != nil {
		t.Fatalf("ParseAgents failed: %v", err)
	}

	if len(policies) != 2 {
		t.Fatalf("Expected 2 policies, got %d", len(policies))
	}

	for i, policy := range policies {
		if policy.Name == "" {
			t.Errorf("Policy %d missing name", i)
		}
		if policy.Message == "" {
			t.Errorf("Policy %d missing message", i)
		}
		if policy.OriginalText == "" {
			t.Errorf("Policy %d missing original text", i)
		}
	}
}
