package tests

import (
	"fmt"
	"log/slog"
	"os"
	"testing"
	"time"

	"github.com/jfraeys/fetch_ml/internal/container"
	"github.com/jfraeys/fetch_ml/internal/logging"
	"github.com/jfraeys/fetch_ml/internal/tracking"
	"github.com/jfraeys/fetch_ml/internal/tracking/factory"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestTrackingIntegration(t *testing.T) {
	if os.Getenv("CI") != "" || os.Getenv("SKIP_E2E") != "" {
		t.Skip("Skipping tracking E2E test in CI")
	}

	logger := logging.NewLogger(slog.LevelDebug, false)
	// ctx := context.Background()

	// 1. Setup Podman Manager
	podmanMgr, err := container.NewPodmanManager(logger)
	require.NoError(t, err)

	// 2. Setup Tracking Registry & Loader
	registry := tracking.NewRegistry(logger)
	loader := factory.NewPluginLoader(logger, podmanMgr)

	// 3. Configure Plugins (Use simple alpine for sidecar test to save time/bandwidth)
	// We'll mimic MLflow using a small image
	plugins := map[string]factory.PluginConfig{
		"mlflow": {
			Enabled:      true,
			Image:        "alpine:latest", // Mock image for speed
			Mode:         "sidecar",
			ArtifactPath: "/tmp/artifacts",
			Settings: map[string]any{
				"tracking_uri": "http://mock:5000",
			},
		},
		"tensorboard": {
			Enabled:     true,
			Image:       "alpine:latest",
			Mode:        "sidecar",
			LogBasePath: "/tmp/logs",
		},
	}

	// 4. Load Plugins
	err = loader.LoadPlugins(plugins, registry)
	require.NoError(t, err)

	// 5. Test Provisioning
	taskID := fmt.Sprintf("test-task-%d", time.Now().Unix())
	_ = taskID // Suppress unused for now

	_ = taskID // Suppress unused for now

	// Provision all (mocks sidecar startup)
	configs := map[string]tracking.ToolConfig{
		"mlflow": {
			Enabled: true,
			Mode:    tracking.ModeSidecar,
			Settings: map[string]any{
				"job_name": "test-job",
			},
		},
		"tensorboard": {
			Enabled: true,
			Mode:    tracking.ModeSidecar,
			Settings: map[string]any{
				"job_name": "test-job",
			},
		},
	}

	// Just verify that the keys in configs align with registered plugins
	for name := range configs {
		_, ok := registry.Get(name)
		assert.True(t, ok, fmt.Sprintf("Plugin %s should be registered", name))
	}

	// For E2E we can try to actually run ProvisionAll if we had mocks or a "dry run" mode.
	// But without mocking podman, it tries to actually run.
	// We'll trust the registration for now as the lighter weight E2E check.

	_, ok := registry.Get("mlflow")
	assert.True(t, ok, "MLflow plugin should be registered")

	_, ok = registry.Get("tensorboard")
	assert.True(t, ok, "TensorBoard plugin should be registered")
}
