import { test, expect } from '@playwright/test'

test.describe('OMS-ORD-003 주문 Excel Import Golden Flow', () => {
  test('파일 → 매핑 → 검증 → 미리보기 → 반영 → 부분성공 복구', async ({ page }) => {
    await page.goto('/oms/orders/import')
    await expect(page.getByRole('heading', { name: '주문 Excel 업로드' })).toBeVisible()

    // Demo API validates the browser workflow, not workbook parsing. Keep this fixture in-memory so
    // the E2E contract never depends on an untracked binary workbook in the repository.
    await page.locator('input[type=file]').setInputFiles({
      name: 'orders-valid-and-invalid.xlsx',
      mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      buffer: Buffer.from('KBX E2E demo workbook'),
    })

    const mappingHeading=page.getByRole('heading',{name:'필드 매핑'})
    await expect(mappingHeading).toBeVisible()
    await expect(mappingHeading).toBeFocused()
    await page.getByRole('button', { name: '검증 시작' }).click()

    const previewHeading=page.getByRole('heading',{name:'반영 미리보기'})
    await expect(previewHeading).toBeVisible({ timeout: 30_000 })
    await expect(previewHeading).toBeFocused()
    await expect(page.getByText('오류').first()).toBeVisible()

    await page.getByRole('button', { name: '첫 오류' }).click()
    await expect(page.locator('[data-import-error-index="0"]')).toBeFocused()

    await page.getByRole('button', { name: '정상 데이터 반영' }).click()
    await page.getByRole('button', { name: /건 반영$/ }).click()

    const resultHeading=page.getByRole('heading',{name:'반영 결과'})
    await expect(resultHeading).toBeVisible({ timeout: 30_000 })
    await expect(resultHeading).toBeFocused()
    await expect(page.getByText(/정상 데이터는 반영되었고 오류 데이터는 제외되었습니다/)).toBeVisible()
    await expect(page.getByRole('button',{name:'오류파일 다운로드'})).toBeVisible()
  })
})
