//go:build integration
package towmanparklocation

import (
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/db"
	"github.com/fiskerinc/cloud-services/pkg/redisv2"
	"github.com/fiskerinc/cloud-services/pkg/utils/localtests"
)


func TestParkLocation(t *testing.T){
	localtests.SetTestENV_Prod(t)
	ivansVin := "VCF1ZBU28PG002114"
	// Add Ivan's Car to tracked vehicles
	db := db.DBClient{}
	_, err := db.GetConn().Exec("INSERT INTO towman.vehicles (vin) VALUES (?)", ivansVin)
	if err != nil {
		t.Fatalf("Failed to insert vehicle: %v", err)
	}
	// Remove Ivan's car from tracked vehicles
	defer func() {
		_, err := db.GetConn().Exec("DELETE FROM towman.vehicles WHERE vin = ?", ivansVin)
		if err != nil {
			t.Fatalf("Failed to delete vehicle: %v", err)
		}		
	}()
	// Send in a location to be parked
	redisv2Client := redisv2.NewClient(nil)
	plt := InitParkLocationTracker(&db, redisv2Client)
	for x := 0; x < 5; x ++{
		plt.recordParkLocation(ivansVin)
	}
	
	// Verify location is stored in parked locations
	var locationRecords []MockLocationStruct
	res, err := db.GetConn().Query(&locationRecords, "SELECT latitude, longitude FROM towman.parked_locations WHERE vin = ? ORDER BY created_at", ivansVin)
	if err != nil {
		t.Fatalf("Failed to query parked locations: %v", err)
	}
	if res.RowsAffected() != 5 {
		t.Fatalf("Expected 5 location records, got %d", res.RowsAffected())
	}

}

type MockLocationStruct struct {
	Latitude  float64 `redis:"latitude"`
	Longitude float64 `redis:"longitude"`
}