package mill

import (
	"context"
	"encoding/json"
	"io"
	"log/slog"
	"net/http"
	"net/http/httptest"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"tangled.org/core/api/tangled"
	"tangled.org/core/notifier"
	"tangled.org/core/spindle/config"
	"tangled.org/core/spindle/db"
	"tangled.org/core/spindle/engines/dummy"
	"tangled.org/core/spindle/mill/executor"
	"tangled.org/core/spindle/models"
)

func TestEndToEndDummyJob(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	l := slog.New(slog.NewTextHandler(io.Discard, nil))

	millDir := t.TempDir()
	bdb, err := db.Make(ctx, filepath.Join(millDir, "mill.db"))
	if err != nil {
		t.Fatalf("mill db: %v", err)
	}
	bn := notifier.New()
	mill := New(l, Config{LogDir: millDir, ReconnectGrace: time.Minute, BidTimeout: 2 * time.Second})
	mill.Attach(bdb, &bn)
	if err := bdb.AddExecutorToken("exec-1", HashToken("test-token"), nil); err != nil {
		t.Fatalf("register executor token: %v", err)
	}

	srv := httptest.NewServer(http.HandlerFunc(mill.HandleExecutorConn))
	defer srv.Close()
	wsURL := "ws" + strings.TrimPrefix(srv.URL, "http")

	execDir := t.TempDir()
	edb, err := db.Make(ctx, filepath.Join(execDir, "exec.db"))
	if err != nil {
		t.Fatalf("exec db: %v", err)
	}
	en := notifier.New()
	cfg := &config.Config{}
	cfg.Server.Dev = true
	cfg.Server.LogDir = execDir
	cfg.Server.Hostname = "exec-1"
	cfg.Mill.URL = wsURL
	cfg.Mill.Seats = 2
	cfg.Mill.SharedSecret = "test-token"

	engines := map[string]models.Engine{"dummy": dummy.New(l)}
	exec := executor.New(cfg, engines, edb, &en, l)
	go exec.Connect(ctx)

	be := NewEngine("dummy", mill)
	twf := tangled.Pipeline_Workflow{
		Name: "build",
		Raw:  "steps:\n  - name: hello\n    command: echo hi\n",
	}
	wf, err := be.InitWorkflow(twf, tangled.Pipeline{})
	if err != nil {
		t.Fatalf("InitWorkflow: %v", err)
	}
	wid := models.WorkflowId{PipelineId: models.PipelineId{Knot: "knot.test", Rkey: "rkey1"}, Name: "build"}

	placeCtx, placeCancel := context.WithTimeout(ctx, 10*time.Second)
	defer placeCancel()

	slot, err := mill.place(placeCtx, "dummy", wid, wf)
	if err != nil {
		t.Fatalf("place: %v", err)
	}
	defer slot.Release()

	if err := mill.commitAndWait(placeCtx, wf, nil); err != nil {
		t.Fatalf("commitAndWait: %v, want success", err)
	}

	if !waitForStatus(t, bdb, wid, "running") {
		t.Fatal("mill never saw relayed running status")
	}
}

func TestExecutorConfiguredLabelsAreStoredOnSession(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	l := slog.New(slog.NewTextHandler(io.Discard, nil))

	millDir := t.TempDir()
	bdb, err := db.Make(ctx, filepath.Join(millDir, "mill.db"))
	if err != nil {
		t.Fatalf("mill db: %v", err)
	}
	bn := notifier.New()
	mill := New(l, Config{LogDir: millDir, ReconnectGrace: time.Minute, BidTimeout: 2 * time.Second})
	mill.Attach(bdb, &bn)
	if err := bdb.AddExecutorToken("exec-labels", HashToken("test-token"), nil); err != nil {
		t.Fatalf("register executor token: %v", err)
	}

	srv := httptest.NewServer(http.HandlerFunc(mill.HandleExecutorConn))
	defer srv.Close()
	wsURL := "ws" + strings.TrimPrefix(srv.URL, "http")

	execDir := t.TempDir()
	edb, err := db.Make(ctx, filepath.Join(execDir, "exec.db"))
	if err != nil {
		t.Fatalf("exec db: %v", err)
	}
	en := notifier.New()
	cfg := &config.Config{}
	cfg.Server.Dev = true
	cfg.Server.LogDir = execDir
	cfg.Server.Hostname = "exec-labels"
	cfg.Mill.URL = wsURL
	cfg.Mill.Seats = 2
	cfg.Mill.SharedSecret = "test-token"
	cfg.Mill.Labels = []string{"linux", "arm64", "gpu"}

	engines := map[string]models.Engine{"dummy": dummy.New(l)}
	exec := executor.New(cfg, engines, edb, &en, l)
	go exec.Connect(ctx)

	if !waitForSessionLabels(t, mill, "exec-labels", []string{"linux", "arm64", "gpu"}) {
		t.Fatal("mill session never stored executor labels from hello")
	}
}

func TestEndToEndDummyJobUsesRequiredLabelsAcrossExecutors(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	l := slog.New(slog.NewTextHandler(io.Discard, nil))

	millDir := t.TempDir()
	bdb, err := db.Make(ctx, filepath.Join(millDir, "mill.db"))
	if err != nil {
		t.Fatalf("mill db: %v", err)
	}
	bn := notifier.New()
	mill := New(l, Config{LogDir: millDir, ReconnectGrace: time.Minute, BidTimeout: 2 * time.Second})
	mill.Attach(bdb, &bn)
	if err := bdb.AddExecutorToken("exec-x86", HashToken("token-x86"), nil); err != nil {
		t.Fatalf("register x86 executor token: %v", err)
	}
	if err := bdb.AddExecutorToken("exec-arm", HashToken("token-arm"), nil); err != nil {
		t.Fatalf("register arm executor token: %v", err)
	}

	srv := httptest.NewServer(http.HandlerFunc(mill.HandleExecutorConn))
	defer srv.Close()
	wsURL := "ws" + strings.TrimPrefix(srv.URL, "http")

	startExecutor := func(name, token string, labels []string) {
		t.Helper()
		execDir := t.TempDir()
		edb, err := db.Make(ctx, filepath.Join(execDir, "exec.db"))
		if err != nil {
			t.Fatalf("%s exec db: %v", name, err)
		}
		en := notifier.New()
		cfg := &config.Config{}
		cfg.Server.Dev = true
		cfg.Server.LogDir = execDir
		cfg.Server.Hostname = name
		cfg.Mill.URL = wsURL
		cfg.Mill.Seats = 1
		cfg.Mill.SharedSecret = token
		cfg.Mill.Labels = labels

		engines := map[string]models.Engine{"dummy": dummy.New(l)}
		exec := executor.New(cfg, engines, edb, &en, l)
		go exec.Connect(ctx)
	}
	startExecutor("exec-x86", "token-x86", []string{"linux/amd64", "kvm"})
	startExecutor("exec-arm", "token-arm", []string{"linux/arm64", "kvm"})

	if !waitForSessionLabels(t, mill, "exec-x86", []string{"linux/amd64", "kvm"}) {
		t.Fatal("x86 executor did not connect with labels")
	}
	if !waitForSessionLabels(t, mill, "exec-arm", []string{"linux/arm64", "kvm"}) {
		t.Fatal("arm executor did not connect with labels")
	}

	be := NewEngine("dummy", mill)
	twf := tangled.Pipeline_Workflow{
		Name:   "build-arm",
		RunsOn: []string{"linux/arm64"},
		Raw:    "steps:\n  - name: hello\n    command: echo hi\n",
	}
	wf, err := be.InitWorkflow(twf, tangled.Pipeline{})
	if err != nil {
		t.Fatalf("InitWorkflow: %v", err)
	}
	wid := models.WorkflowId{PipelineId: models.PipelineId{Knot: "knot.test", Rkey: "rkey-arm"}, Name: "build-arm"}

	placeCtx, placeCancel := context.WithTimeout(ctx, 10*time.Second)
	defer placeCancel()

	slot, err := mill.place(placeCtx, "dummy", wid, wf)
	if err != nil {
		t.Fatalf("place: %v", err)
	}
	defer slot.Release()

	lease := wf.Data.(*millWorkflowState).Lease
	if lease == nil {
		t.Fatal("place did not attach lease to workflow state")
	}
	if lease.nodeID != "exec-arm" {
		t.Fatalf("placed on %q, want exec-arm", lease.nodeID)
	}

	if err := mill.commitAndWait(placeCtx, wf, nil); err != nil {
		t.Fatalf("commitAndWait: %v, want success", err)
	}
}

func waitForSessionLabels(t *testing.T, m *Mill, nodeID string, want []string) bool {
	t.Helper()
	deadline := time.Now().Add(3 * time.Second)
	for time.Now().Before(deadline) {
		m.mu.Lock()
		sess := m.sessions[nodeID]
		var got []string
		if sess != nil {
			got = append([]string(nil), sess.labels...)
		}
		m.mu.Unlock()
		if sameStringMultiset(got, want) {
			return true
		}
		time.Sleep(10 * time.Millisecond)
	}
	return false
}

func waitForStatus(t *testing.T, d *db.DB, wid models.WorkflowId, want string) bool {
	t.Helper()
	deadline := time.Now().Add(3 * time.Second)
	aturi := string(wid.PipelineId.AtUri())
	for time.Now().Before(deadline) {
		evs, err := d.GetEvents(0, 1000)
		if err != nil {
			t.Fatalf("GetEvents: %v", err)
		}
		for _, ev := range evs {
			if ev.Nsid != tangled.PipelineStatusNSID {
				continue
			}
			var st tangled.PipelineStatus
			if err := json.Unmarshal(ev.EventJson, &st); err != nil {
				continue
			}
			if st.Pipeline == aturi && st.Workflow == wid.Name && st.Status == want {
				return true
			}
		}
		time.Sleep(50 * time.Millisecond)
	}
	return false
}
