import { test, expect } from '@playwright/test'
import { requireKbxScenario, scenarioTitle } from './helpers/kbxScenario'

const queueRecovery=requireKbxScenario('scenario.common.operations.queue-recovery')

test.describe('COMMON-OPS-001 업무 예외 센터', () => {
  test(scenarioTitle(queueRecovery.id), async ({ page }) => {
    await page.goto('/operations/exceptions')
    await expect(page.locator('[data-screen-id="COMMON-OPS-001"]')).toBeVisible()
    await expect(page.locator('[data-kbx-surface="exception-summary"]')).toBeVisible()
    await expect(page.locator('[data-kbx-surface="queue/content"]')).toBeVisible()
    await expect(page.getByText(/정상 건은 표시하지 않습니다/)).toBeVisible()
  })

  test('HTTP 200이어도 해결 거절이면 성공으로 가장하지 않고 authoritative receipt로 복구한다', async ({ page }) => {
    await page.route('**/api/operations/work-items/resolve', route => route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify({ resolvedCount: 0, rejectedCount: 1 }),
    }))

    await page.goto('/operations/exceptions')
    await page.getByText('주문 재고 부족', { exact: true }).dblclick()
    await expect(page.getByRole('button', { name: '해결 처리' })).toBeVisible()
    await page.getByRole('button', { name: '해결 처리' }).click()

    const receipt=page.locator('[data-kbx-surface="authoritative-action-receipt"]')
    await expect(receipt).toBeVisible()
    await expect(receipt).toBeFocused()
    await expect(receipt).toContainText('해결 처리가 완료되지 않았습니다.')
    await expect(receipt).toContainText('해결 0건')
    await expect(receipt).toContainText('거절 1건')
    await expect(page.getByRole('button', { name: '최신 상태 조회' })).toBeVisible()
  })

  test('다음 긴급건은 demo DTO의 critical work item을 실제 Drawer로 연다', async ({ page }) => {
    await page.goto('/operations/exceptions')
    const nextUrgent=page.getByRole('button', { name: '다음 긴급건' })
    await expect(nextUrgent).toBeEnabled()
    await nextUrgent.click()
    await expect(page.getByText('출고 연계 지연', { exact: true })).toBeVisible()
    await expect(page.getByRole('button', { name: '재처리' })).toBeVisible()
  })
})
