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

test.use({ storageState: { cookies: [], origins: [] } })

test.describe('shared session smoke tests', () => {
  test('a public share opens for an unauthenticated viewer', async ({ page }) => {
    const api = await createAuthenticatedApiContext()
    const tree = await createTroubleshootingTree(api, {
      name: uniqueName('Playwright Shared Session Flow'),
    })
    const session = await createSession(api, tree.id, {
      ticket_number: `PW-SHARE-${Date.now()}`,
      client_name: 'Shared Session Client',
    })
    await completeSession(api, session.id, {
      outcome: 'resolved',
      outcome_notes: 'Shared session smoke test',
    })
    const share = await createSessionShare(api, session.id, {
      visibility: 'public',
      share_name: 'Playwright Customer Share',
    })

    try {
      await page.goto(`/share/${share.share_token}`)

      await expect(
        page.getByRole('heading', { name: 'Playwright Customer Share' }),
      ).toBeVisible()
      await expect(page.getByText('Can you reproduce the issue?', { exact: true })).toBeVisible()
      await expect(page.getByText(`Ticket: #${session.ticket_number}`)).toBeVisible()
      await expect(page.getByText(`Client: ${session.client_name}`)).toBeVisible()
    } finally {
      await disposeApiContext(api)
    }
  })
})
