import { expect, test } from '@playwright/test'
import {
  completeSession,
  createAuthenticatedApiContext,
  createSession,
  createTroubleshootingTree,
  disposeApiContext,
  uniqueName,
} from './helpers/api'

test.describe('analytics smoke tests', () => {
  test('personal and team analytics pages load for a team admin', async ({ page }) => {
    const api = await createAuthenticatedApiContext()
    const tree = await createTroubleshootingTree(api, {
      name: uniqueName('Playwright Analytics Flow'),
    })
    const session = await createSession(api, tree.id, {
      ticket_number: `PW-ANALYTICS-${Date.now()}`,
      client_name: 'Analytics Client',
    })
    await completeSession(api, session.id, {
      outcome: 'resolved',
      outcome_notes: 'Analytics smoke test session',
    })

    try {
      await page.goto('/analytics/me')

      await expect(
        page.getByRole('heading', { name: 'My Analytics' }),
      ).toBeVisible()
      await expect(page.getByText('My Sessions', { exact: true })).toBeVisible()

      await page.goto('/analytics')

      await expect(
        page.getByRole('heading', { name: 'Team Analytics' }),
      ).toBeVisible()
      await expect(page.getByText('Total Sessions', { exact: true })).toBeVisible()
    } finally {
      await disposeApiContext(api)
    }
  })
})
