//go:build e2e

package onboarding

import (
	"context"
	"testing"
	"time"

	"github.com/tidbcloud/AgentTeam/e2e/assertions"
	"github.com/tidbcloud/AgentTeam/e2e/testkit"
)

func TestOnboardingSmoke(t *testing.T) {
	env := testkit.StartEnvironment(t)
	user := testkit.RequirePrimaryUser(t, env)

	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	workspaces, status, raw, err := user.API.ListWorkspaces(ctx)
	if err != nil {
		t.Fatalf("list workspaces: %v", err)
	}
	assertions.RequireStatus(t, status, 200, raw)
	t.Logf("listed workspaces: count=%d status=%d", len(workspaces), status)

	if len(workspaces) == 0 {
		t.Fatalf("expected at least one workspace")
	}
	t.Logf("onboarding smoke passed")
}

func TestQuickstartAllChannelMessage(t *testing.T) {
	sc := testkit.NewSingleUserScenario(t)

	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	allChannel := testkit.RequireAllChannel(t, sc.API)
	t.Logf("found default #all channel: id=%s", allChannel.ID)

	content := testkit.UniqueMessageText("quickstart-all")
	sent, status, raw, err := sc.API.CreateMessage(ctx, allChannel.ID, content)
	if err != nil {
		t.Fatalf("send #all message: %v", err)
	}
	assertions.RequireStatus(t, status, 200, raw)
	if sent.ChannelID != allChannel.ID || sent.Content != content {
		t.Fatalf("unexpected send response: channelId=%s content=%q", sent.ChannelID, sent.Content)
	}

	testkit.AssertMessageInChannel(t, sc.API, allChannel.ID, content)
	t.Logf("quickstart #all message delivered: messageId=%s", sent.ID)
}

func TestRegisterProfileSmoke(t *testing.T) {
	testkit.SkipUnlessEmbedded(t, "registration smoke requires embedded profile without preset tokens")

	env := testkit.StartEnvironment(t)
	registered := testkit.RequireRegisteredUser(t, env, "profile-user")
	api := env.API.WithToken(registered.AccessToken)

	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	profile, status, raw, err := api.GetMe(ctx)
	if err != nil {
		t.Fatalf("get profile: %v", err)
	}
	assertions.RequireStatus(t, status, 200, raw)
	if profile.ID != registered.User.ID {
		t.Fatalf("profile id mismatch: got %s want %s", profile.ID, registered.User.ID)
	}
	if profile.Email == "" || profile.Name == "" {
		t.Fatalf("incomplete profile: %+v", profile)
	}
	t.Logf("profile smoke passed: userId=%s email=%s", profile.ID, profile.Email)
}

func TestOnboardingWorkspaceAndAllChannelMatrix(t *testing.T) {
	sc := testkit.NewSingleUserScenario(t)
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
	defer cancel()

	var allChannelID string
	var sentContent string
	t.Run("01-workspace-list-contains-current-workspace", func(t *testing.T) {
		workspaces, status, raw, err := sc.User.API.ListWorkspaces(ctx)
		if err != nil {
			t.Fatalf("list workspaces: %v", err)
		}
		assertions.RequireStatus(t, status, 200, raw)
		if len(workspaces) == 0 {
			t.Fatalf("expected at least one workspace")
		}
		found := false
		for _, ws := range workspaces {
			if ws.ID == sc.Workspace.ID {
				found = true
				break
			}
		}
		if !found {
			t.Fatalf("current workspace %s not found in workspace list", sc.Workspace.ID)
		}
	})

	t.Run("02-default-all-channel-is-visible", func(t *testing.T) {
		allChannel := testkit.RequireAllChannel(t, sc.API)
		if allChannel.ID == "" {
			t.Fatalf("default #all channel id should not be empty")
		}
		allChannelID = allChannel.ID
	})

	t.Run("03-send-all-message-and-verify-history", func(t *testing.T) {
		sentContent = testkit.UniqueMessageText("onboarding-matrix-all-msg")
		sent, status, raw, err := sc.API.CreateMessage(ctx, allChannelID, sentContent)
		if err != nil {
			t.Fatalf("send message to #all: %v", err)
		}
		assertions.RequireStatus(t, status, 200, raw)
		if sent.ChannelID != allChannelID || sent.Content != sentContent {
			t.Fatalf("unexpected sent payload: %+v", sent)
		}
		testkit.AssertMessageInChannel(t, sc.API, allChannelID, sentContent)
	})

	t.Run("04-workspace-members-after-onboarding-not-empty", func(t *testing.T) {
		page, status, raw, err := sc.User.API.ListWorkspaceMembers(ctx, sc.Workspace.ID)
		if err != nil {
			t.Fatalf("list workspace members: %v", err)
		}
		assertions.RequireStatus(t, status, 200, raw)
		if len(page.Members) == 0 {
			t.Fatalf("expected non-empty workspace members after onboarding")
		}
	})

	t.Run("05-workspace-usage-available", func(t *testing.T) {
		usage, status, raw, err := sc.User.API.GetWorkspaceUsage(ctx, sc.Workspace.ID)
		if err != nil {
			t.Fatalf("get workspace usage: %v", err)
		}
		assertions.RequireStatus(t, status, 200, raw)
		if len(usage) == 0 {
			t.Fatalf("expected non-empty usage payload")
		}
	})
}
