import { test, expect } from "@playwright/test";
import { login } from "@open-mercato/core/modules/core/__integration__/helpers/auth";
test.describe("TC-AI-sharing-07: viewer sees Owner label on owner messages", () => {
  const STUB_CONV_ID = "test-shared-conv-02";
  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: (/* @__PURE__ */ new Date()).toISOString(),
      isOwner: false
    },
    messages: [
      {
        id: "msg-u1",
        clientMessageId: "cmsg-u1",
        role: "user",
        content: "What are the open deals for Acme?",
        uiParts: [],
        attachmentIds: [],
        files: [],
        model: null,
        metadata: null,
        createdAt: (/* @__PURE__ */ new Date()).toISOString(),
        // Deliberately null — imported messages never have senderUserId.
        senderUserId: null
      },
      {
        id: "msg-a1",
        clientMessageId: "cmsg-a1",
        role: "assistant",
        content: "I found 3 open deals for Acme Corp.",
        uiParts: [],
        attachmentIds: [],
        files: [],
        model: "claude-haiku-4-5",
        metadata: null,
        createdAt: (/* @__PURE__ */ new Date()).toISOString(),
        senderUserId: null
      }
    ],
    nextCursor: null
  };
  test("viewer sees Owner/Assistant labels and read-only notice", 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: 200, contentType: "application/json", body: JSON.stringify(conversationStub) })
    );
    await page.goto(`/backend?openAiConversation=${STUB_CONV_ID}`, { waitUntil: "domcontentloaded" });
    const sheet = page.locator("[data-ai-launcher-sheet]");
    await expect(sheet).toBeVisible({ timeout: 1e4 });
    await expect(sheet.locator("[data-ai-chat-read-only-notice]")).toBeVisible();
    const userMessages = sheet.locator('[data-role="user"]');
    await expect(userMessages.first()).toBeVisible();
    const userLabel = userMessages.first().locator("div.text-xs").first();
    await expect(userLabel).toHaveText(/owner/i);
    await expect(userLabel).not.toHaveText(/^you$/i);
    const assistantMessages = sheet.locator('[data-role="assistant"]');
    await expect(assistantMessages.first()).toBeVisible();
    const assistantLabel = assistantMessages.first().locator("div.text-xs").first();
    await expect(assistantLabel).toHaveText(/assistant/i);
  });
  test("viewer composer is hidden (cannot send messages)", 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: 200, contentType: "application/json", body: JSON.stringify(conversationStub) })
    );
    await page.goto(`/backend?openAiConversation=${STUB_CONV_ID}`, { waitUntil: "domcontentloaded" });
    const sheet = page.locator("[data-ai-launcher-sheet]");
    await expect(sheet).toBeVisible({ timeout: 1e4 });
    const composer = sheet.locator("form[data-ai-chat-composer], [data-ai-chat-composer]");
    const isComposerVisible = await composer.isVisible().catch(() => false);
    expect(isComposerVisible).toBe(false);
  });
});
//# sourceMappingURL=TC-AI-sharing-07-owner-label.spec.js.map
