package handlers

import (
	"testing"

	"qctextbuilder/internal/domain"
)

func TestExtractBlockID(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name string
		f    domain.TemplateField
		want string
	}{
		{
			name: "from key",
			f: domain.TemplateField{
				KeyName: "textTitle_m1710_1",
				Path:    "text.textTitle_m1710_1",
			},
			want: "m1710",
		},
		{
			name: "from path",
			f: domain.TemplateField{
				KeyName: "servicesTitle_8",
				Path:    "services.servicesTitle_r4830_8",
			},
			want: "r4830",
		},
		{
			name: "none",
			f: domain.TemplateField{
				KeyName: "plainTitle",
				Path:    "text.plainTitle",
			},
			want: "",
		},
	}

	for _, tc := range tests {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			got := extractBlockID(tc.f)
			if got != tc.want {
				t.Fatalf("extractBlockID() = %q, want %q", got, tc.want)
			}
		})
	}
}

func TestApplyTextGroupingPrefersBlockID(t *testing.T) {
	t.Parallel()

	section := buildFieldSectionView{Key: "text", Title: "Text"}
	fields := []pendingField{
		{
			Field: domain.TemplateField{
				Section:      "text",
				KeyName:      "textTitle_m1710_1",
				Path:         "text.textTitle_m1710_1",
				DisplayLabel: "text.textTitle_m1710_1",
			},
			View: buildFieldView{Path: "text.textTitle_m1710_1"},
		},
		{
			Field: domain.TemplateField{
				Section:      "ext",
				KeyName:      "textDescription_c7886_3",
				Path:         "ext.textDescription_c7886_3",
				DisplayLabel: "ext.textDescription_c7886_3",
			},
			View: buildFieldView{Path: "ext.textDescription_c7886_3"},
		},
		{
			Field: domain.TemplateField{
				Section:      "text",
				KeyName:      "textDescription_m1710_2",
				Path:         "text.textDescription_m1710_2",
				DisplayLabel: "text.textDescription_m1710_2",
			},
			View: buildFieldView{Path: "text.textDescription_m1710_2"},
		},
	}

	got := applyTextGrouping(section, fields)
	if len(got.EditableGroups) != 2 {
		t.Fatalf("expected 2 block groups, got %d", len(got.EditableGroups))
	}
	if got.EditableGroups[0].Title != "Hero / Haupttitel (m1710)" {
		t.Fatalf("unexpected first group title: %q", got.EditableGroups[0].Title)
	}
	if len(got.EditableGroups[0].Fields) != 2 {
		t.Fatalf("expected 2 fields in first group, got %d", len(got.EditableGroups[0].Fields))
	}
	if got.EditableGroups[1].Title != "Intro / Einleitung (c7886)" {
		t.Fatalf("unexpected second group title: %q", got.EditableGroups[1].Title)
	}
}

func TestPreferredBuildSectionUsesWebsiteSectionFirst(t *testing.T) {
	t.Parallel()

	field := domain.TemplateField{
		Section:        "text",
		WebsiteSection: domain.WebsiteSectionCTA,
		KeyName:        "textTitle",
		Path:           "text.textTitle",
	}
	got := preferredBuildSection(field)
	if got != domain.WebsiteSectionCTA {
		t.Fatalf("preferredBuildSection() = %q, want %q", got, domain.WebsiteSectionCTA)
	}
}

func TestParseAutofillAction(t *testing.T) {
	t.Parallel()

	action, field := parseAutofillAction("apply_field::text.textTitle_m1710_1")
	if action != "apply_field" {
		t.Fatalf("expected action apply_field, got %q", action)
	}
	if field != "text.textTitle_m1710_1" {
		t.Fatalf("expected field path parsed, got %q", field)
	}
}

func TestFieldAnchorID(t *testing.T) {
	t.Parallel()

	got := fieldAnchorID("text.textTitle_m1710_1")
	if got != "field-text-texttitle-m1710-1" {
		t.Fatalf("unexpected anchor id: %q", got)
	}
}
