import { expect, test, type Page } from "@playwright/test";

/**
 * The Phase 7 acceptance path, driven entirely through the dashboard:
 * register → create an agent → connect Slack → build & run a workflow →
 * read the audit trail. No direct API calls.
 */
function nav(page: Page, name: string) {
  // Scope navigation to the sidebar so it doesn't collide with in-page links.
  return page.getByRole("complementary").getByRole("link", { name, exact: true });
}

test("a user can drive the full core path from the UI", async ({ page }) => {
  const stamp = Date.now();
  const email = `e2e_${stamp}@legate.dev`;

  // -- Register --
  await page.goto("/register");
  await page.getByPlaceholder("Ada Lovelace").fill("E2E Operator");
  await page.getByPlaceholder("you@company.com").fill(email);
  await page.getByPlaceholder("••••••••").fill("password123");
  await page.getByRole("button", { name: "Create account" }).click();

  // First-run: create a workspace.
  await expect(page.getByText("Create your workspace")).toBeVisible({ timeout: 15_000 });
  await page.getByPlaceholder("Acme Inc.").fill(`E2E ${stamp}`);
  await page.getByRole("button", { name: "Create", exact: true }).click();
  await expect(page.getByRole("heading", { name: "Command deck" })).toBeVisible({ timeout: 15_000 });

  // -- Create an agent --
  await nav(page, "Agents").click();
  await page.getByRole("link", { name: "New agent" }).first().click();
  await page.getByPlaceholder("Support triage").fill("Triage Bot");
  await page.getByRole("button", { name: /kb\.search/ }).first().click();
  await page.getByRole("button", { name: "Create agent" }).click();
  await expect(page.getByRole("heading", { name: "Triage Bot" })).toBeVisible({ timeout: 15_000 });

  // -- Connect Slack --
  await nav(page, "Connectors").click();
  await page.getByRole("button", { name: /Add connector/ }).first().click();
  const dialog = page.getByRole("dialog");
  await dialog.getByText("Slack", { exact: true }).click();
  await dialog.getByLabel(/bot_token/).fill("xoxb-e2e");
  await dialog.getByRole("button", { name: "Install connector" }).click();
  await expect(page.getByText("Northwind", { exact: false }).or(page.getByText("Slack")).first()).toBeVisible({
    timeout: 15_000,
  });

  // -- Build & run a workflow (no LLM needed) --
  await nav(page, "Workflows").click();
  await page.getByRole("button", { name: "New workflow" }).first().click();
  await page.getByPlaceholder("Triage inbound").fill("Ping KB");
  await page.getByRole("button", { name: "Create workflow" }).click();
  await expect(page.getByText("Ping KB")).toBeVisible({ timeout: 15_000 });
  await page.getByRole("button", { name: "Run", exact: true }).first().click();
  await expect(page.getByRole("heading", { name: /run/i })).toBeVisible({ timeout: 15_000 });

  // -- Read the audit trail --
  await nav(page, "Audit").click();
  await expect(page.getByRole("heading", { name: "Audit log" })).toBeVisible();
  await expect(page.getByText("workflow.run")).toBeVisible({ timeout: 15_000 });
});
