import { test, expect } from "@playwright/test";
import { login } from "@open-mercato/core/modules/core/__integration__/helpers/auth";
test.describe("TC-AI-sharing-06: deep-link auto-open from ?openAiConversation", () => {
  const STUB_CONV_ID = "test-shared-conv-01";
  const STUB_AGENT_ID = "customers.account_assistant";
  const agentsStub = {
    agents: [
      {
        id: STUB_AGENT_ID,
        moduleId: "customers",
        label: "Account assistant",
        description: "Helps with customer accounts.",
        executionMode: "chat",
        mutationPolicy: "read-only",
        allowedTools: [],
        requiredFeatures: [],
        acceptedMediaTypes: [],
        hasOutputSchema: false
      }
    ],
    total: 1,
    aiConfigured: true
  };
  const conversationStub = {
    conversation: {
      conversationId: STUB_CONV_ID,
      agentId: STUB_AGENT_ID,
      title: "Shared conversation",
      status: "open",
      visibility: "shared",
      pageContext: null,
      createdAt: (/* @__PURE__ */ new Date()).toISOString(),
      updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
      lastMessageAt: null,
      importedFromLocalAt: null,
      isOwner: false
    },
    messages: [],
    nextCursor: null
  };
  async function setupStubs(page) {
    await page.route(
      "**/api/ai_assistant/health",
      (route) => route.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify({ healthy: true }) })
    );
    await page.route(
      "**/api/ai_assistant/ai/agents",
      (route) => route.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify(agentsStub) })
    );
    await page.route(
      `**/api/ai_assistant/ai/conversations/${STUB_CONV_ID}**`,
      (route) => route.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify(conversationStub) })
    );
  }
  test("chat sheet opens when page loads with ?openAiConversation param", async ({ page }) => {
    await login(page, "superadmin");
    await setupStubs(page);
    await page.goto(`/backend?openAiConversation=${STUB_CONV_ID}`, { waitUntil: "domcontentloaded" });
    await expect(page.locator("[data-ai-launcher-sheet]")).toBeVisible({ timeout: 1e4 });
  });
  test("URL param is stripped after deep-link is handled", async ({ page }) => {
    await login(page, "superadmin");
    await setupStubs(page);
    await page.goto(`/backend?openAiConversation=${STUB_CONV_ID}`, { waitUntil: "domcontentloaded" });
    await expect(page.locator("[data-ai-launcher-sheet]")).toBeVisible({ timeout: 1e4 });
    await expect(page).not.toHaveURL(/openAiConversation/, { timeout: 5e3 });
  });
  test("deep-link is ignored when conversation fetch returns 403", async ({ page }) => {
    await login(page, "superadmin");
    await page.route(
      "**/api/ai_assistant/health",
      (route) => route.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify({ healthy: true }) })
    );
    await page.route(
      "**/api/ai_assistant/ai/agents",
      (route) => route.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify(agentsStub) })
    );
    await page.route(
      `**/api/ai_assistant/ai/conversations/${STUB_CONV_ID}**`,
      (route) => route.fulfill({ status: 403, contentType: "application/json", body: JSON.stringify({ error: "forbidden" }) })
    );
    await page.goto(`/backend?openAiConversation=${STUB_CONV_ID}`, { waitUntil: "domcontentloaded" });
    await expect(page.locator("[data-ai-launcher-trigger]")).toBeVisible({ timeout: 1e4 });
    await expect(page.locator("[data-ai-launcher-sheet]")).not.toBeVisible();
  });
});
//# sourceMappingURL=TC-AI-sharing-06-deep-link.spec.js.map
