package e2e_test

import (
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
)

func TestTicketManagerE2EScriptHelpAndDryRun(t *testing.T) {
	t.Parallel()

	scriptPath := filepath.Join("..", "..", "..", "scripts", "test", "ticket-manager-e2e.sh")
	if _, err := os.Stat(scriptPath); err != nil {
		t.Fatalf("expected e2e script at %s: %v", scriptPath, err)
	}

	helpOutput, err := exec.Command("bash", scriptPath, "--help").CombinedOutput()
	if err != nil {
		t.Fatalf("run script --help: %v\n%s", err, string(helpOutput))
	}
	helpText := string(helpOutput)
	for _, expected := range []string{"ticket-manager e2e", "--cleanup", "--mode", "workspace-timeline"} {
		if !strings.Contains(helpText, expected) {
			t.Fatalf("expected help output to mention %q, got %q", expected, helpText)
		}
	}
	for _, removed := range []string{"conversation", "jira-search", "lark-chat-summary"} {
		if strings.Contains(helpText, removed) {
			t.Fatalf("expected removed case %q to be absent from help, got %q", removed, helpText)
		}
	}

	dryRunOutput, err := exec.Command("bash", scriptPath, "--dry-run").CombinedOutput()
	if err != nil {
		t.Fatalf("run script --dry-run: %v\n%s", err, string(dryRunOutput))
	}
	dryRunText := string(dryRunOutput)
	for _, expected := range []string{
		"ticket-manager e2e dry-run",
		".tmp/ticket-manager-e2e",
		"agent_gateway_log=",
		"e2e_mode=docker",
		"app_env=local",
		"dockerfile=",
		"image_tag=",
		"mysql_tls_config_key=",
		"e2e_ca_cert_file=",
	} {
		if !strings.Contains(dryRunText, expected) {
			t.Fatalf("expected dry-run output to mention %q, got %q", expected, dryRunText)
		}
	}

	tidbDryRun := exec.Command("bash", scriptPath, "--dry-run")
	tidbDryRun.Env = append(os.Environ(), "MYSQL_HOST=gateway01.us-west-2.prod.aws.tidbcloud.com")
	tidbDryRunOutput, err := tidbDryRun.CombinedOutput()
	if err != nil {
		t.Fatalf("run script --dry-run with TiDB Cloud host: %v\n%s", err, string(tidbDryRunOutput))
	}
	if !strings.Contains(string(tidbDryRunOutput), "mysql_tls_config_key=true") {
		t.Fatalf("expected TiDB Cloud host to default mysql TLS config key to true, got %q", string(tidbDryRunOutput))
	}
}

func TestTicketManagerE2EHasWorkspaceTimelineCaseOnly(t *testing.T) {
	t.Parallel()

	root := filepath.Join("..", "..", "..")
	for _, path := range []string{
		filepath.Join(root, "scripts", "test", "lib", "ticket-manager-e2e-common.sh"),
		filepath.Join(root, "scripts", "test", "e2e-cases", "workspace-timeline.sh"),
		filepath.Join(root, "scripts", "test", "ticket-manager-e2e.sh"),
		filepath.Join(root, "scripts", "test", "ticket-manager-workspace-timeline-e2e.sh"),
	} {
		if _, err := os.Stat(path); err != nil {
			t.Fatalf("expected e2e file at %s: %v", path, err)
		}
	}

	for _, removed := range []string{
		filepath.Join(root, "scripts", "test", "e2e-cases", "conversation.sh"),
		filepath.Join(root, "scripts", "test", "e2e-cases", "jira-search.sh"),
		filepath.Join(root, "scripts", "test", "e2e-cases", "lark-chat-summary.sh"),
		filepath.Join(root, "scripts", "test", "ticket-manager-conversation-e2e.sh"),
		filepath.Join(root, "scripts", "test", "ticket-manager-jira-search-e2e.sh"),
		filepath.Join(root, "scripts", "test", "jira-search-skill-e2e.sh"),
	} {
		if _, err := os.Stat(removed); !os.IsNotExist(err) {
			t.Fatalf("expected old-endpoint e2e file to be removed: %s", removed)
		}
	}

	commonPath := filepath.Join(root, "scripts", "test", "lib", "ticket-manager-e2e-common.sh")
	commonBytes, err := os.ReadFile(commonPath)
	if err != nil {
		t.Fatalf("read common e2e harness: %v", err)
	}
	commonText := string(commonBytes)
	for _, removed := range []string{"/api/v1/ticketmanager/conversations", "post_cli_conversation", "export_jira_env_from_base_config"} {
		if strings.Contains(commonText, removed) {
			t.Fatalf("expected old conversation helper %q to be removed", removed)
		}
	}
	if !strings.Contains(commonText, "SSL_CERT_FILE") {
		t.Fatal("expected common harness to support custom CA cert propagation")
	}

	workspaceTimelinePath := filepath.Join(root, "scripts", "test", "e2e-cases", "workspace-timeline.sh")
	workspaceTimelineBytes, err := os.ReadFile(workspaceTimelinePath)
	if err != nil {
		t.Fatalf("read workspace timeline e2e case: %v", err)
	}
	workspaceTimelineText := string(workspaceTimelineBytes)
	for _, expected := range []string{
		"run_workspace_timeline_case()",
		"workspace_timeline_evidence",
		"workspace_timeline_entries",
		"workspace_timeline_entry_evidence",
		"workspace_timeline_sync_states",
		"workspace_timeline_entry_revisions",
		"/curated-timeline",
		"/timeline-evidence",
		"/timeline-sync",
		"task_update",
		"workspace-curated-timeline-dedup-check",
	} {
		if !strings.Contains(workspaceTimelineText, expected) {
			t.Fatalf("expected workspace timeline e2e case to include %q", expected)
		}
	}
}
