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

/**
 * E2E: Nexus onboarding wizard — single-step root directory flow.
 *
 * Verifies:
 *   ONBD-10 — Vite alias intercepts, NexusOnboardingWizard renders
 *   ONBD-11 — Single root-directory input only, no multi-step flow
 *   ONBD-12 — No corporate placeholder text visible
 */

test.describe("Nexus onboarding wizard", () => {
  test("single-step flow: root dir input, no corporate strings, lands on dashboard", async ({ page }) => {
    await page.goto("/");

    // ONBD-10 + ONBD-11: Nexus wizard renders with single-step heading
    const heading = page.locator("h1", { hasText: "Welcome to Nexus" });
    await expect(heading).toBeVisible({ timeout: 15_000 });

    // ONBD-11: Only a root directory input — no multi-step navigation
    await expect(page.getByRole("button", { name: "Next" })).toHaveCount(0);
    await expect(page.locator("h3", { hasText: "Name your company" })).toHaveCount(0);
    await expect(page.locator("h3", { hasText: "Create your first agent" })).toHaveCount(0);
    await expect(page.locator("h3", { hasText: "Give it something to do" })).toHaveCount(0);
    await expect(page.locator("h3", { hasText: "Ready to launch" })).toHaveCount(0);

    // ONBD-12: No corporate placeholder text
    await expect(page.getByText("Acme Corp")).toHaveCount(0);
    await expect(page.getByText("Company name")).toHaveCount(0);
    await expect(page.getByText("What is this company trying to achieve?")).toHaveCount(0);

    // Fill root directory and submit
    const rootDirInput = page.locator('input[placeholder="~/projects/my-project"]');
    await expect(rootDirInput).toBeVisible();
    await rootDirInput.fill("/tmp/nexus-e2e-test");

    await page.getByRole("button", { name: "Get Started" }).click();

    // Should navigate to dashboard
    await expect(page).toHaveURL(/\/dashboard/, { timeout: 30_000 });

    // Verify workspace and agents created via API
    const baseUrl = page.url().split("/").slice(0, 3).join("/");

    const companiesRes = await page.request.get(`${baseUrl}/api/companies`);
    expect(companiesRes.ok()).toBe(true);
    const companies = await companiesRes.json();
    expect(companies.length).toBeGreaterThan(0);

    const companyId = companies[0].id;
    const agentsRes = await page.request.get(
      `${baseUrl}/api/companies/${companyId}/agents`
    );
    expect(agentsRes.ok()).toBe(true);
    const agents = await agentsRes.json();

    // PM agent (role: ceo, name: "Project Manager") and Engineer created
    expect(
      agents.some((a: { name: string }) => a.name === "Project Manager")
    ).toBe(true);
    expect(
      agents.some((a: { name: string }) => a.name === "Engineer")
    ).toBe(true);
  });
});
