import { expect, test } from '@playwright/test'
import { startFixtureCollector, type FixtureCollector } from './fixture-collector'
import { ADAPTER_STATUSES, BOTS_PANEL } from './fixtures/collector-fixtures'

test.describe.configure({ mode: 'serial' })

let collector: FixtureCollector

const OBSERVABILITY_BOTS_PANEL = {
  ...BOTS_PANEL,
  data: {
    bots: [
      {
        id: 'dealbot',
        name: 'dealbot',
        project: 'deals',
        runtimeStatus: 'running',
        status: 'live',
        statusDetail: 'running · live',
        messages24h: 412,
        errors: 0,
        cost: 1.9,
        messagesTotal: 1200,
        costTotal: 5.4,
        lastActivityAt: new Date().toISOString(),
        lastMessageAt: new Date().toISOString(),
      },
      {
        id: 'hetzibot',
        name: 'HetziBot',
        project: 'Hetzi',
        runtimeStatus: 'running',
        status: 'down',
        statusDetail: 'running · last call 56d ago',
        messages24h: 0,
        errors: 0,
        cost: null,
        messagesTotal: 124,
        costTotal: 0.0003,
        lastActivityAt: '2026-06-09T12:25:14.044Z',
        lastMessageAt: '2026-06-09T12:25:14.044Z',
      },
      {
        id: 'zyncbot',
        name: 'ZyncBot',
        project: 'Zync.is',
        runtimeStatus: 'running',
        status: 'idle',
        statusDetail: 'running · idle',
        messages24h: 0,
        errors: 0,
        cost: null,
        messagesTotal: 0,
        costTotal: null,
        lastActivityAt: null,
        lastMessageAt: null,
      },
    ],
  },
} as const

test.beforeAll(async () => {
  collector = await startFixtureCollector({
    panels: [OBSERVABILITY_BOTS_PANEL],
    adapters: ADAPTER_STATUSES.filter((a) => a.id === 'botmaster'),
    items: [],
  })
})

test.afterAll(async () => {
  await collector.close()
})

test('/bots renders fleet summary and full observability table', async ({ page }) => {
  await page.goto('/bots', { waitUntil: 'domcontentloaded' })

  await expect(page.getByRole('heading', { level: 4, name: 'Fleet summary' })).toBeVisible()
  await expect(page.getByText('Reporting', { exact: true })).toBeVisible()
  await expect(page.getByText('2/3', { exact: true })).toBeVisible()

  await expect(page.getByRole('heading', { level: 4, name: 'Bots (Botmaster)' })).toBeVisible()
  await expect(page.getByRole('columnheader').filter({ hasText: 'Last active' })).toBeVisible()
  await expect(page.getByRole('columnheader').filter({ hasText: 'Last message' })).toBeVisible()
  await expect(page.getByRole('searchbox', { name: 'Filter bots' })).toBeVisible()
  await expect(page.getByText('Total', { exact: true })).toBeVisible()

  await expect(page.getByText('HetziBot')).toBeVisible()
  await expect(page.getByText('ZyncBot')).toBeVisible()
  await expect(page.getByText('running · idle')).toBeVisible()
  await expect(page.getByText(/running · last call 56d ago/)).toBeVisible()
  await expect(page.getByRole('row', { name: /HetziBot/ })).toContainText('124')
})

test('overview Bots card shows bot names, not opaque ids', async ({ page }) => {
  await page.goto('/', { waitUntil: 'domcontentloaded' })
  await expect(page.getByRole('heading', { level: 4, name: /^Bots \(Botmaster\)/ })).toBeVisible()
  await expect(page.getByText('HetziBot')).toBeVisible()
  await expect(page.getByText('4laMbKGL04')).toHaveCount(0)
})
