package file_correction

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
)

// tagID 23948 ValidateTaskFileOrder. Валидация файла. Success path.
func TestFileService_ValidateFileOrder_Success(t *testing.T) {
	csvContent := `Receipt External ID,otherColumn
id1,data1
id1,data2
id2,data3
id2,data4
id3,data5`

	s := storageFile{
		sourceFile: strings.NewReader(csvContent),
		closer: func() error {
			return nil
		},
		linesLimit: 5,
	}

	err := s.Validate()
	assert.NoError(t, err)
}

// tagID 23948 ValidateTaskFileOrder. Валидация файла. Неверный порядок Receipt External ID.
func TestFileService_ValidateFileOrder_FailOrder(t *testing.T) {
	// Test for incorrect order
	csvContentIncorrect := `Receipt External ID,otherColumn
id1,data1
id2,data2
id1,data3`

	s := storageFile{
		sourceFile: strings.NewReader(csvContentIncorrect),
		closer: func() error {
			return nil
		},
		linesLimit: 3,
	}

	err := s.Validate()
	assert.Error(t, err)
	assert.ErrorIs(t, err, ErrValidation)
}

// tagID 23948 ValidateFileLineCount. Валидация файла. Пройдена проверка лимита строк.
func TestFileService_ValidateFileLineCount_Success(t *testing.T) {
	csvContent := `Receipt External ID,otherColumn
id1,data1
id1,data2
id2,data3`

	s := storageFile{
		sourceFile: strings.NewReader(csvContent),
		closer: func() error {
			return nil
		},
		linesLimit: 10,
	}

	err := s.Validate()
	assert.NoError(t, err)
}

// tagID 23948 ValidateFileLineCount. Валидация файла. Лимит строк превышен.
func TestFileService_ValidateFileLineCount_FileTooLong(t *testing.T) {
	longCSVContent := `Receipt External ID,otherColumn
id1,data1
id1,data2
id1,data3
id1,data4
id1,data5
id2,data6
id2,data7
id2,data8
id2,data9`

	s := storageFile{
		sourceFile: strings.NewReader(longCSVContent),
		closer: func() error {
			return nil
		},
		linesLimit: 5,
	}

	err := s.Validate()
	assert.Error(t, err)
	assert.ErrorIs(t, err, ErrValidation)
}

// tagID 23948 ValidateTaskFileOrder. Валидация файла. Нет заголовка receipt external ID.
func TestFileService_ValidateFileLineCount_NoHeaderReceiptExternalID(t *testing.T) {
	csvContent := `Receipt ID,otherColumn
id1,data1
id1,data2
id2,data3`

	s := storageFile{
		sourceFile: strings.NewReader(csvContent),
		closer: func() error {
			return nil
		},
		linesLimit: 3,
	}

	err := s.Validate()
	assert.ErrorIs(t, err, ErrValidation)
}

по примеру этого

разметть мне тегами (tagID ...) айди тот же

вот этот файл

package file_correction

import (
	"os"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestImportFileV2_Validation(t *testing.T) {
	t.Parallel()
	tests := []struct {
		name        string
		fileContent []byte
		wantErr     bool
	}{
		{
			name: "success",
			fileContent: []byte(
				`Receipt External ID,otherColumn
id1,data1
id1,data2
id2,data3
id2,data4
id3,data5`,
			),
			wantErr: false,
		},
		{
			name:        "empty",
			fileContent: nil,
			wantErr:     true,
		},
		{
			name: "without receipt id header",
			fileContent: []byte(
				`Receipt ID,otherColumn
id1,data1
id2,data2
id1,data3`,
			),
			wantErr: true,
		},
		{
			name: "unequal number of fields",
			fileContent: []byte(
				`Receipt External ID,otherColumn
id1,data1
id1,data2
id2,data3, extradata
id2,data4
id3`,
			),
			wantErr: true,
		},
		{
			name: "no receipt id field",
			fileContent: []byte(
				`Receipt External ID,otherColumn
id1,data1
id1,data2
,data3
id2,data4
id3,data5`,
			),
			wantErr: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			file, err := os.CreateTemp("", "TestImportFileV2_ValidationFile-*.tmp")
			assert.NoError(t, err)

			defer func() {
				assert.NoError(t, file.Close())
				assert.NoError(t, os.Remove(file.Name()))
			}()

			_, err = file.Write(tt.fileContent)
			assert.NoError(t, err)

			info, err := file.Stat()
			assert.NoError(t, err)

			importFile := importFileV2{
				file: file,
				info: info,
			}

			if !tt.wantErr {
				assert.NoError(t, importFile.Validate())
			} else {
				assert.Error(t, importFile.Validate())
			}
		})
	}
}