package utm

import (
	"testing"

	"apps/backend/internal/config"
	"apps/backend/internal/module/growth/domain"

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

func TestTrackUrlIsAbsoluteSoItCanBePastedIntoAPost(t *testing.T) {
	var c config.Config
	c.PublicAPIBase = "https://api.example.com/"
	c.PublicWebBase = "https://app.example.com"

	out := publicView(c, &domain.UtmLink{ID: "l1", Code: "abc123", DestinationURL: "https://shop.example.com/p/1"})

	require.Equal(t, "https://api.example.com/api/v1/public/u/abc123", out.TrackUrl)
	require.Equal(t, "/api/v1/public/u/abc123", out.TrackPath)
}

func TestTrackUrlFallsBackToWebBaseWhichProxiesApi(t *testing.T) {
	var c config.Config
	c.PublicWebBase = "http://127.0.0.1:5173"

	out := publicView(c, &domain.UtmLink{Code: "xyz"})

	require.Equal(t, "http://127.0.0.1:5173/api/v1/public/u/xyz", out.TrackUrl)
}

func TestTrackUrlStaysRelativeWhenNoBaseIsConfigured(t *testing.T) {
	out := publicView(config.Config{}, &domain.UtmLink{Code: "xyz"})

	require.Equal(t, "/api/v1/public/u/xyz", out.TrackUrl)
}
