import { test, expect } from '@playwright/test'

test.describe('Agent Health (/agents)', () => {
  test('renders stat cards and group cards', async ({ page }) => {
    await page.goto('/agents')

    // Stat strip
    await expect(page.getByText('Total Agents')).toBeVisible()
    await expect(page.getByText('Total TPS')).toBeVisible()

    // Group cards for each application
    await expect(page.getByText('order-service').first()).toBeVisible()
    await expect(page.getByText('payment-svc').first()).toBeVisible()
    await expect(page.getByText('notification-hub').first()).toBeVisible()

    // Instance tables have data
    const instanceRows = page.locator('table tbody tr')
    expect(await instanceRows.count()).toBeGreaterThan(0)

    // Instance table headers
    await expect(page.getByRole('columnheader', { name: 'Instance' }).first()).toBeVisible()
    await expect(page.getByRole('columnheader', { name: 'State' }).first()).toBeVisible()
    await expect(page.getByRole('columnheader', { name: 'Uptime' }).first()).toBeVisible()
    await expect(page.getByRole('columnheader', { name: 'TPS' }).first()).toBeVisible()

    // Timeline section
    await expect(page.getByText('Timeline').first()).toBeVisible()
  })

  test('clicking an instance row opens the detail panel', async ({ page }) => {
    await page.goto('/agents')

    // Click first instance row
    const instanceRow = page.locator('table tbody tr').first()
    await instanceRow.click()

    // Detail panel opens — look for detail-specific labels
    await expect(page.getByText('Version').first()).toBeVisible()
    await expect(page.getByText('Throughput').first()).toBeVisible()
  })

  test('detail panel has Performance tab with charts', async ({ page }) => {
    await page.goto('/agents')

    // Click an instance to open detail panel
    const instanceRow = page.locator('table tbody tr').first()
    await instanceRow.click()

    // Wait for panel to open
    await expect(page.getByText('Version').first()).toBeVisible()

    // DetailPanel tabs are plain buttons (not role="tab")
    // Switch to Performance tab
    const perfTab = page.getByRole('button', { name: 'Performance' })
    await perfTab.click()

    // Performance charts should render
    await expect(page.getByText('Throughput (msg/s)').first()).toBeVisible()
    await expect(page.getByText('Error Rate (err/h)').first()).toBeVisible()
  })

  test('app-scoped agents view', async ({ page }) => {
    await page.goto('/agents/order-service')

    // Breadcrumb/scope shows app
    await expect(page.getByLabel('Breadcrumb').getByText('Agents')).toBeVisible()

    // Only order-service agents should show
    await expect(page.getByText('ord-1').first()).toBeVisible()
    await expect(page.getByText('ord-2').first()).toBeVisible()
    await expect(page.getByText('ord-3').first()).toBeVisible()
  })

  test('dead agent shows alert banner', async ({ page }) => {
    await page.goto('/agents')

    // notification-hub has a dead instance, should show alert
    await expect(page.getByText('Single point of failure')).toBeVisible()
  })
})
