package remotefileupload

import (
	"context"
	"sync"
	"testing"
)

var (
	guard = make(chan struct{}, 100)
)

func TestAzureBlobURL(t *testing.T) {
	backup := NewBackup("", "", "")
	var inputs = []struct {
		base     string
		filePath string
		expected string
	}{
		{ // Test case 1: basePath is empty, filePath is empty
			base:     "",
			filePath: "",
			expected: "",
		},
		{ // Test case 2: basePath is not empty, filePath is empty
			base:     "/base",
			filePath: "",
			expected: "/base",
		},
		{ // Test case 3: basePath is not empty, filePath is not empty
			base:     "/base",
			filePath: "dir1/dir2",
			expected: "/base/dir1/dir2",
		},
		{ // Test case 4: basePath is empty, filePath is not empty
			base:     "",
			filePath: "dir1/dir2/dir3/raw.csv",
			expected: "dir1/dir2/dir3/raw.csv",
		},
		{ // Test case 4: worng basePath, filePath file path
			base:     "/base//",
			filePath: "//dir1/dir2/dir3/raw.csv",
			expected: "/base/dir1/dir2/dir3/raw.csv",
		},
	}

	for _, input := range inputs {
		result := backup.azureBlobURL(input.base, input.filePath)
		if result != input.expected {
			t.Errorf("Expected %s, got %s", input.expected, result)
		}
	}

}

func TestRemove(t *testing.T) {
	t.Skip()
	ctx := context.Background()
	backup := NewBackup("fakeAccount", "fakeAccountKey", "fakeContainer")
	fakePath := "fakeDir1/fakeDir2/file.txt"

	err := backup.remove(ctx, fakePath)

	if err != nil {
		t.Errorf("Expected no error, got %v", err.Error())
	}

}

func TestSetTTL(t *testing.T) {
	t.Skip()
	ctx := context.Background()
	backup := NewBackup("fakeAccount", "fakeAccountKey", "fakeContainer")
	fakeFileUrl := "https://fakeAccount.blob.core.windows.net/fakeContainer/fakeDir1/fakeDir2/file.txt"

	err := backup.setTTL(ctx, fakeFileUrl)

	if err != nil {
		t.Errorf("Expected no error, got %v", err.Error())
	}

}

func TestGetContainerPath(t *testing.T) {
	inputes := []struct {
		containerName string
		expected      string
	}{
		{"container1", "https://fakeAccount.blob.core.windows.net/container1/"},
		{"container2", "https://fakeAccount.blob.core.windows.net/container2/"},
		{"container3", "https://fakeAccount.blob.core.windows.net/container3/"},
	}
	backup := NewBackup("fakeAccount", "fakeAccountKey", "fakeContainer")

	for _, input := range inputes {
		result := backup.getContainerPath(input.containerName)
		if result != input.expected {
			t.Errorf("Expected %v, got %v", input.expected, result)
		}
	}
}

func TestMove(t *testing.T) {
	t.Skip()
	ctx := context.Background()
	backup := NewBackup("fakeAccount", "fakeAccountKey", "fakeContainer")

	fakeFilePath := "fakeDir1/fakeDir2/file.txt"

	err := backup.Move(ctx, fakeFilePath)

	if err != nil {
		t.Errorf("Expected no error, got %v", err.Error())
	}
}

func TestChangeFileExt(t *testing.T) {
	backup := NewBackup("fakeAccount", "fakeAccountKey", "fakeContainer")
	inputs := []struct {
		fileUrl  string
		ext      string
		expected string
	}{
		{"document.pdf", "txt", "document.txt"},
		{"dir1/dir2/document.txt", "pdf", "dir1/dir2/document.pdf"},
		{"", "pdf", ".pdf"},
		{"document", "txt", "document.txt"},
		{"document", ".txt", "document.txt"},
		{"document.txt", ".pdf", "document.pdf"},
		{"https://fakeAccount.blob.core.windows.net/fakeContainer/fakeVin/fakeVersion/yyyy/mm/dd/raw.csv", ".parquet", "https://fakeAccount.blob.core.windows.net/fakeContainer/fakeVin/fakeVersion/yyyy/mm/dd/raw.parquet"},
		{"fakeVin/fakeVersion/yyyy/mm/dd/file.txt", "pdf", "fakeVin/fakeVersion/yyyy/mm/dd/file.pdf"},
	}

	for _, input := range inputs {
		result := backup.changeFileExt(input.fileUrl, input.ext)
		if result != input.expected {
			t.Errorf("Expected %v, got %v", input.expected, result)
		}
	}
}

func TestToParquet(t *testing.T) {

	t.Skip()
	backup := NewBackup("fakeAccount", "fakeAccountKey", "fakeContainer")

	fakeFilePath := []string{
		"fakeVin1/fakeVersion1/yyyy/mm/dd/raw.csv",
		"fakeVin2/fakeVersion2/yyyy/mm/dd/raw.csv",
		"fakeVin3/fakeVersio3/yyyy/mm/dd/raw.csv",
	}

	var wg sync.WaitGroup
	for _, url := range fakeFilePath {
		wg.Add(1)
		go func(url string) {
			defer wg.Done()
			err := backup.ToParquet(url, guard)
			if err != nil {
				t.Errorf("Expected nil error, got %v", err.Error())
			}
		}(url)
	}

	wg.Wait()

}
