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

const mismatchRecovery=requireKbxScenario('scenario.common.reconcile.mismatch-recovery')

test.describe('COMMON-REC-001 업무 데이터 대사', () => {
  test(scenarioTitle(mismatchRecovery.id), async ({ page }) => {
    await page.goto('/operations/reconcile')
    await expect(page.locator('[data-screen-id="COMMON-REC-001"]')).toBeVisible()
    await expect(page.locator('[data-kbx-surface="criteria/search"]')).toBeVisible()
    await expect(page.locator('[data-kbx-surface="comparison-grid"]')).toBeVisible()
    await expect(page.getByText('ORD-260811-0001', { exact: true })).toBeVisible()
  })

  test('예외등록 HTTP 200의 skippedCount를 실제 업무 결과로 표시하고 원천값은 변경하지 않는다', async ({ page }) => {
    await page.route('**/api/reconcile/items/create-exceptions', route => route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify({ createdOrUpdatedCount: 0, skippedCount: 1 }),
    }))

    await page.goto('/operations/reconcile')
    await page.getByText('ORD-260811-0001', { exact: true }).dblclick()
    await expect(page.getByText(/원천값을 화면에서 직접 덮어쓰지 않습니다/)).toBeVisible()
    await page.getByRole('button', { name: '이 건 예외 등록' }).click()

    const receipt=page.locator('[data-kbx-surface="reconcile-authoritative-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()
  })
})
