package remotefileupload_test

import (
	"fmt"
	"testing"
	"time"

	"github.com/fiskerinc/cloud-services/pkg/remotefileupload"
	"github.com/fiskerinc/cloud-services/pkg/utils/envtool"

	"github.com/fiskerinc/cloud-services/pkg/testhelper"
)

// In order to run the integration tests on the uploader, the go routine that makes the upload needs to be not a goroutine
func TestNewAzureUploaderIntegration(t *testing.T) {
	t.Skip()

	azureContainerName := envtool.GetEnv("AZURE_TREX_LOGS_STORAGE_CONTAINER_NAME", "raw-can")
	_, err := remotefileupload.NewAzureUploader(azureContainerName, ".csv")
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestNewAzureUploaderIntegration", nil, err)
		return
	}
}

func TestAzureUploadIntegration(t *testing.T) {
	t.Skip()

	azureContainerName := envtool.GetEnv("AZURE_TREX_LOGS_STORAGE_CONTAINER_NAME", "raw-can")
	a, err := remotefileupload.NewAzureUploader(azureContainerName, ".csv")
	if err != nil {
		t.Error(err)
		return
	}

	date := fmt.Sprintf("%04d/%02d/%02d", time.Now().Year(), time.Now().Month(), time.Now().Day())
	_, err = a.Upload([]byte("{'id':'testJson'}"), remotefileupload.LogPayload{Title: "vin", Value: "TESTVIN123"}, "TESTVIN123", date)
	if err != nil {
		t.Error(err)
		return
	}
}

func TestAzureUploadTestAppend(t *testing.T) {
	t.Skip()
	a, err := remotefileupload.NewAzureUploader("trex-logs", ".csv")
	if err != nil {
		t.Fatal(err)
	}

	_, err = a.Upload([]byte("Hello"), remotefileupload.LogPayload{Title: "vin", Value: "path"}, "path")
	if err != nil {
		t.Fatal(err)
	}

	_, err = a.Upload([]byte("goodbye"), remotefileupload.LogPayload{Title: "vin", Value: "path"}, "path")
	if err != nil {
		t.Fatal(err)
	}

	_, err = a.Upload([]byte("again"), remotefileupload.LogPayload{Title: "vin", Value: "path"}, "path")
	if err != nil {
		t.Fatal(err)
	}

	time.Sleep(time.Minute)
}

func TestURLGeneration(t *testing.T) {
	link, err := remotefileupload.AzureFilePathLink("dev-account", "trex-logs", "trex", "12345678", "2022", "log.txt")
	if err != nil {
		t.Error(err)
	}

	if link != "https://dev-account.blob.core.windows.net/trex-logs/trex/12345678/2022/log.txt" {
		t.Errorf("Link did not match: %s", link)
	}

	link, err = remotefileupload.AzureFilePathLink("dev-account", "trex-logs", "trex", "/12345678/2022", "log.txt")
	if err != nil {
		t.Error(err)
	}

	if link != "https://dev-account.blob.core.windows.net/trex-logs/trex/12345678/2022/log.txt" {
		t.Errorf("Link did not match: %s", link)
	}
}
