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

test.describe('session workflow smoke tests', () => {
  test('can start and complete a troubleshooting session through the UI', async ({ page }) => {
    const api = await createAuthenticatedApiContext()
    const tree = await createTroubleshootingTree(api, {
      question: 'Did the restart resolve the issue?',
      answerLabel: 'Yes, it is fixed',
      solutionTitle: 'Document the fix and close the ticket',
    })

    try {
      await page.goto(`/trees/${tree.id}/navigate`)

      await expect(
        page.getByRole('heading', { name: tree.name }),
      ).toBeVisible()

      await page.getByPlaceholder('e.g., INC0012345').fill('PW-START-COMPLETE')
      await page.getByPlaceholder('e.g., Acme Corp').fill('Workflow Client')
      await page.getByRole('button', { name: 'Start Troubleshooting' }).click()

      await expect(
        page.getByRole('heading', { name: 'Did the restart resolve the issue?' }),
      ).toBeVisible()

      await page.getByRole('button', { name: 'Yes, it is fixed' }).click()

      await expect(
        page.getByRole('heading', { name: 'Document the fix and close the ticket' }),
      ).toBeVisible()

      await page.getByRole('button', { name: 'Complete Session' }).click()

      const outcomeDialog = page.getByRole('dialog', { name: 'Session Outcome' })
      await expect(outcomeDialog).toBeVisible()
      await outcomeDialog.getByPlaceholder('Add context for this outcome...').fill('Playwright verified the UI completion flow.')
      await outcomeDialog.getByRole('button', { name: 'Complete Session' }).click()

      const csatDialog = page.getByRole('dialog', { name: 'How was this flow?' })
      await expect(csatDialog).toBeVisible()
      await csatDialog.getByRole('button', { name: 'Skip' }).click()

      await expect(page).toHaveURL(/\/sessions\/[0-9a-f-]+$/)
      await expect(page.getByText('Resolved')).toBeVisible()
      await expect(page.getByText('PW-START-COMPLETE')).toBeVisible()
    } finally {
      await disposeApiContext(api)
    }
  })

  test('can preview an export for a completed session', async ({ page }) => {
    const api = await createAuthenticatedApiContext()
    const tree = await createTroubleshootingTree(api, {
      name: 'Playwright Export Flow',
    })
    const session = await createSession(api, tree.id, {
      ticket_number: 'PW-EXPORT',
      client_name: 'Export Client',
    })
    await completeSession(api, session.id, {
      outcome_notes: 'Completed for export preview verification',
    })

    try {
      await page.goto(`/sessions/${session.id}`)

      await expect(
        page.getByRole('heading', { name: 'PW-EXPORT' }),
      ).toBeVisible()

      await page.getByRole('button', { name: 'Preview' }).click()

      const previewDialog = page.getByRole('dialog', { name: 'Export Preview' })
      await expect(previewDialog).toBeVisible()
      await expect(previewDialog.getByLabel('Export content')).not.toHaveValue('')
      await expect(previewDialog.getByLabel('Export content')).toHaveValue(/PW-EXPORT/)
    } finally {
      await disposeApiContext(api)
    }
  })
})
