package sheets

import "testing"

func TestBuildWriteRange(t *testing.T) {
	tests := []struct {
		name      string
		sheetID   string
		startCell string
		values    [][]string
		want      string
		wantErr   bool
	}{
		{
			name:      "single cell",
			sheetID:   "sheet1",
			startCell: "A1",
			values:    [][]string{{"x"}},
			want:      "sheet1!A1:A1",
		},
		{
			name:      "multiple rows and columns",
			sheetID:   "93dbfb",
			startCell: "A1",
			values:    [][]string{{"h1", "h2", "h3"}, {"v1", "v2", "v3"}},
			want:      "93dbfb!A1:C2",
		},
		{
			name:      "offset start cell",
			sheetID:   "sheet1",
			startCell: "B2",
			values:    [][]string{{"h1", "h2"}, {"v1"}},
			want:      "sheet1!B2:C3",
		},
		{
			name:      "column after z",
			sheetID:   "sheet1",
			startCell: "Z1",
			values:    [][]string{{"h1", "h2"}},
			want:      "sheet1!Z1:AA1",
		},
		{
			name:      "empty values",
			sheetID:   "sheet1",
			startCell: "A1",
			values:    nil,
			wantErr:   true,
		},
		{
			name:      "invalid start cell",
			sheetID:   "sheet1",
			startCell: "1A",
			values:    [][]string{{"x"}},
			wantErr:   true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := buildWriteRange(tt.sheetID, tt.startCell, tt.values)
			if (err != nil) != tt.wantErr {
				t.Fatalf("buildWriteRange() error = %v, wantErr %v", err, tt.wantErr)
			}
			if got != tt.want {
				t.Fatalf("buildWriteRange() = %q, want %q", got, tt.want)
			}
		})
	}
}
