package vehicleconfig

import (
	"encoding/csv"
	"encoding/json"
	"log"
	"os"
	"slices"
	"testing"
)

/*
1 - Create a directory here: /vin_lists
2 - In that directory place CSV files that consist of newline-delimited lists of VINs.
	These can be generated by PGAdmin. Just remove the quotes around each VIN.
	cec_prd_vins.csv
	cec_euprd_vins.csv
	preprod_vins.csv
	dev_vins.csv
3 - Remove t.Skip() from the desired test below.
4 - Run the test and wait. It will take several hours.
*/

type VinSAPData struct {
	vin        string
	configData string
}

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

	t.Setenv("SAP_URL", "https://tpapi.fiskerinc.com/connected-car-mule")
	t.Setenv("SAP_VEHICLESPEC_API_KEY", "3d5c948d-9bcc-47cf-ab7d-800f8ff4236c")

	//
	// For all NA prod vins
	//

	naVinsFile, err := os.Open("vin_lists/cec_prd_vins.csv")
	if err != nil {
		log.Fatal(err)
	}
	defer naVinsFile.Close()

	allNAVins, err := csv.NewReader(naVinsFile).ReadAll()
	if err != nil {
		log.Fatalln("csv failed", err)
	}

	allNAVinsSlice := slices.Concat(allNAVins...)

	naOutFile, err := os.Create("all_cec_prd_config_data.csv")
	if err != nil {
		log.Fatalln("failed to open file", err)
	}
	defer naOutFile.Close()

	w := csv.NewWriter(naOutFile)
	defer w.Flush()

	getDataAndWriteFile(allNAVinsSlice, w)

	//
	// For all EU prod vins
	//

	euVinsFile, err := os.Open("vin_lists/cec_euprd_vins.csv")
	if err != nil {
		log.Fatal(err)
	}
	defer euVinsFile.Close()

	allEUVins, err := csv.NewReader(euVinsFile).ReadAll()
	if err != nil {
		log.Fatalln("csv failed", err)
	}

	allEUVinsSlice := slices.Concat(allEUVins...)

	euOutFile, err := os.Create("all_cec_euprd_config_data.csv")
	if err != nil {
		log.Fatalln("failed to open file", err)
	}
	defer euOutFile.Close()

	w = csv.NewWriter(euOutFile)
	defer w.Flush()

	getDataAndWriteFile(allEUVinsSlice, w)
}

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

	t.Setenv("SAP_URL", "https://stg.tpapi.fiskerinc.com/connected-car-mule")
	t.Setenv("SAP_VEHICLESPEC_API_KEY", "ff2406ad-de67-479e-aac7-c9681f9e767c")

	//
	// For all preprod vins
	//

	preprdVinsFile, err := os.Open("vin_lists/preprod_vins.csv")
	if err != nil {
		log.Fatal(err)
	}
	defer preprdVinsFile.Close()

	allPreprodVins, err := csv.NewReader(preprdVinsFile).ReadAll()
	if err != nil {
		log.Fatalln("csv failed", err)
	}

	allPreprodVinsSlice := slices.Concat(allPreprodVins...)

	preprodOutFile, err := os.Create("all_preprd_config_data.csv")
	if err != nil {
		log.Fatalln("failed to open file", err)
	}
	defer preprodOutFile.Close()

	w := csv.NewWriter(preprodOutFile)
	defer w.Flush()

	getDataAndWriteFile(allPreprodVinsSlice, w)
}

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

	t.Setenv("SAP_URL", "https://dev.tpapi.fiskerinc.com/connected-car-mule")
	t.Setenv("SAP_VEHICLESPEC_API_KEY", "330e9277-eb7c-4604-934c-45f67d1c98b3")

	//
	// For all dev vins
	//

	devVinsFile, err := os.Open("vin_lists/dev_vins.csv")
	if err != nil {
		log.Fatal(err)
	}
	defer devVinsFile.Close()

	allDevVins, err := csv.NewReader(devVinsFile).ReadAll()
	if err != nil {
		log.Fatalln("csv failed", err)
	}

	allDevVinsSlice := slices.Concat(allDevVins...)

	devOutFile, err := os.Create("all_dev_config_data.csv")
	if err != nil {
		log.Fatalln("failed to open file", err)
	}
	defer devOutFile.Close()

	w := csv.NewWriter(devOutFile)
	defer w.Flush()

	getDataAndWriteFile(allDevVinsSlice, w)
}

func getDataAndWriteFile(vins []string, w *csv.Writer) {
	for _, vin := range vins {
		res, err := NewSAPService().GetFeatureCodes(vin)
		if err != nil {
			log.Fatalln("sap failed", err)
		}

		if len(res.Features) == 0 {
			continue
		}

		sapJSON, err := json.Marshal(res)
		if err != nil {
			log.Fatal(err)
		}

		sapData := VinSAPData{
			vin:        vin,
			configData: string(sapJSON),
		}

		row := []string{sapData.vin, sapData.configData}
		if err := w.Write(row); err != nil {
			log.Fatalln("error writing record to file", err)
		}
	}
}
