import { test, expect } from '@playwright/test';
import { login, PORT_SLUG } from './helpers';

test.describe('Berth detail - Interests tab (Phase B)', () => {
  test.beforeEach(async ({ page }) => {
    await login(page, 'super_admin');
  });

  test('berth detail page exposes an Interests tab', async ({ page }) => {
    // Navigate to the berths list, then click the first row to drill in.
    // The list uses TanStack Table row-click handlers, so we wait for the
    // table body to populate rather than for `a[href*="/berths/"]`.
    await page.goto(`/${PORT_SLUG}/berths`);
    const firstRow = page.locator('tbody tr').first();
    await expect(firstRow).toBeVisible({ timeout: 20_000 });
    // The list table uses an `onRowClick` handler on the <tr> that calls
    // `router.push`. Open the row's actions menu and click "View details"
    // - the menu item's handler routes the same way and is more reliable
    // than firing a synthetic <tr> click under React 19 + dev-mode HMR.
    await firstRow.getByRole('button', { name: /open menu/i }).click();
    await page.getByRole('menuitem', { name: /view details/i }).click();
    await page.waitForURL(/\/berths\/[^/]+$/, { timeout: 10_000 });

    const tab = page.getByRole('tab', { name: 'Interests', exact: true });
    await expect(tab).toBeVisible({ timeout: 10_000 });
    await tab.click();
    await expect(tab).toHaveAttribute('data-state', 'active');

    // Confirm the new tab body replaced the old stub. The body might be:
    //   - a populated table
    //   - the empty-state ("No interests linked to this berth")
    //   - the loading skeleton (still fetching)
    // The negative assertion against the old stub copy is the primary signal.
    await expect(page.getByText('Interests coming soon')).not.toBeVisible({ timeout: 2_000 });
  });
});
