package naming

import (
	"strings"
	"testing"
)

func ptr(i int) *int { return &i }

func TestRender(t *testing.T) {
	tests := []struct {
		name string
		in   Fields
		want string
	}{
		{
			name: "movie with director and year",
			in:   Fields{Type: typeMovie, Title: "Дюна: Часть вторая", Director: "Дени Вильнёв", Year: 2024},
			want: "Дюна: Часть вторая (Дени Вильнёв, 2024)",
		},
		{
			name: "movie with year only",
			in:   Fields{Type: typeMovie, Title: "Дюна: Часть вторая", Year: 2024},
			want: "Дюна: Часть вторая (2024)",
		},
		{
			name: "movie with director only",
			in:   Fields{Type: typeMovie, Title: "Брат", Director: "Алексей Балабанов"},
			want: "Брат (Алексей Балабанов)",
		},
		{
			name: "movie without director and year",
			in:   Fields{Type: typeMovie, Title: "Брат"},
			want: "Брат",
		},
		{
			name: "series with season",
			in:   Fields{Type: typeSeries, Title: "Сёгун", Year: 2024, Season: ptr(2)},
			want: "Сёгун (2024). Сезон 2",
		},
		{
			name: "series without season",
			in:   Fields{Type: typeSeries, Title: "Сёгун", Year: 2024},
			want: "Сёгун (2024)",
		},
		{
			name: "series season zero is omitted",
			in:   Fields{Type: typeSeries, Title: "Сёгун", Season: ptr(0)},
			want: "Сёгун",
		},
		{
			name: "empty title yields empty name",
			in:   Fields{Type: typeMovie, Title: "   ", Year: 2024},
			want: "",
		},
		{
			name: "control chars and newlines are sanitized",
			in:   Fields{Type: typeMovie, Title: "Дюна\n\tЧасть\x00 вторая", Year: 2024},
			want: "Дюна Часть вторая (2024)",
		},
	}
	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			if got := render(tc.in); got != tc.want {
				t.Errorf("render() = %q, want %q", got, tc.want)
			}
		})
	}
}

func TestRenderTruncates(t *testing.T) {
	long := strings.Repeat("я", maxNameLen+50)
	got := render(Fields{Type: typeMovie, Title: long})
	if n := len([]rune(got)); n > maxNameLen {
		t.Errorf("длина имени = %d рун, want <= %d", n, maxNameLen)
	}
}
