package src_test

import (
	"fmt"
	"testing"
)

// section: attr
func Test_Attributes(t *testing.T) {
	// Emit structured key-value pairs to the test log
	t.Attr("test_type", "unit")
	t.Attr("component", "user-service")
	t.Attr("priority", "high")

	// Run the actual test
	result := processUser("test@example.com")
	if !result {
		t.Error("processUser failed")
	}

	t.Log("Test completed with attributes")
}

// section: attr

// section: output-writer
func Test_Output(t *testing.T) {
	// Get an io.Writer that writes to the test log
	// without file:line annotations
	w := t.Output()

	// Write directly to test output
	fmt.Fprintln(w, "Custom output without annotations")
	fmt.Fprintf(w, "User count: %d\n", 42)

	t.Log("This line has the standard annotation")
}

// section: output-writer

// section: subtest-attr
func Test_SubtestAttributes(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected bool
		priority string
	}{
		{"valid_email", "user@example.com", true, "high"},
		{"invalid_email", "not-an-email", false, "medium"},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			// Each subtest can have its own attributes
			t.Attr("input", tc.input)
			t.Attr("priority", tc.priority)

			result := validateEmail(tc.input)
			if result != tc.expected {
				t.Errorf("validateEmail(%q) = %v, want %v",
					tc.input, result, tc.expected)
			}
		})
	}
}

// section: subtest-attr

// Helper functions for tests
func processUser(email string) bool {
	return email != ""
}

func validateEmail(email string) bool {
	// Simple validation for demo
	for _, c := range email {
		if c == '@' {
			return true
		}
	}
	return false
}
