import { test, expect } from '@playwright/test';
import { loginAs, TEST_USER } from './helpers/auth.js';

test.describe('Chat page', () => {
  test.beforeEach(async ({ page }) => {
    await loginAs(page, TEST_USER.email, TEST_USER.password);
    // If login failed (no seeded user in env) we may be on /login — skip
    const url = page.url();
    test.skip(!url.includes('/chat'), 'No seeded test user — skipping authenticated tests');
  });

  test('chat page loads and shows the welcome message or conversation list', async ({ page }) => {
    await page.goto('/chat');
    // Either there are conversations listed or the welcome empty-state is shown
    const hasWelcome = await page
      .getByRole('heading', { name: /welcome to mosaic chat/i })
      .isVisible()
      .catch(() => false);
    const hasConversationPanel = await page
      .locator('[data-testid="conversation-list"], nav, aside')
      .first()
      .isVisible()
      .catch(() => false);

    expect(hasWelcome || hasConversationPanel).toBe(true);
  });

  test('new conversation button is visible', async ({ page }) => {
    await page.goto('/chat');
    // "Start new conversation" button or a "+" button in the sidebar
    const newConvButton = page.getByRole('button', { name: /new conversation|start new/i }).first();
    await expect(newConvButton).toBeVisible({ timeout: 10_000 });
  });

  test('clicking new conversation shows a chat input area', async ({ page }) => {
    await page.goto('/chat');
    // Find any button that creates a new conversation
    const newBtn = page.getByRole('button', { name: /new conversation|start new/i }).first();
    await newBtn.click();
    // After creating, a text input for sending messages should appear
    const chatInput = page.getByRole('textbox').or(page.locator('textarea')).first();
    await expect(chatInput).toBeVisible({ timeout: 10_000 });
  });

  test('sidebar navigation is present on chat page', async ({ page }) => {
    await page.goto('/chat');
    // The app-shell sidebar should be visible
    await expect(page.getByRole('link', { name: /chat/i }).first()).toBeVisible();
  });
});
