import { describe, expect, it } from 'vitest'
import type { KbxImportDefinition, KbxImportMapping } from '@kbx/contracts'
import { validateKbxImportFileCandidate, validateKbxImportMappings } from '@kbx/ui'

const definition:KbxImportDefinition={
  id:'orders',screenId:'OMS-ORD-003',entity:'order',title:'주문 Excel 업로드',allowCreate:true,allowUpdate:true,maxFileSizeBytes:1048576,
  fields:[
    {key:'orderNo',label:'주문번호',dataType:'text',required:true,importable:true},
    {key:'itemCode',label:'품목코드',dataType:'code',required:true,importable:true},
    {key:'remark',label:'비고',dataType:'text',importable:true},
  ],
}

describe('KbxExcelImport guard contract',()=>{
  it('rejects missing required targets and duplicate target mappings before server validation',()=>{
    const mappings:KbxImportMapping[]=[
      {sourceColumn:'주문번호',targetField:'orderNo',source:'exact'},
      {sourceColumn:'상품번호',targetField:'orderNo',source:'manual'},
    ]
    expect(validateKbxImportMappings(definition,mappings).map(x=>x.code)).toEqual(['duplicate-target','required-missing'])
  })

  it('accepts one-to-one required mappings',()=>{
    const mappings:KbxImportMapping[]=[
      {sourceColumn:'주문번호',targetField:'orderNo',source:'exact'},
      {sourceColumn:'상품번호',targetField:'itemCode',source:'alias'},
    ]
    expect(validateKbxImportMappings(definition,mappings)).toEqual([])
  })

  it('preflights xlsx extension and configured upload size without parsing business rows',()=>{
    expect(validateKbxImportFileCandidate(definition,{name:'orders.csv',size:100})).toContain('.xlsx')
    expect(validateKbxImportFileCandidate(definition,{name:'orders.xlsx',size:2097152})).toContain('1MB')
    expect(validateKbxImportFileCandidate(definition,{name:'orders.xlsx',size:512})).toBeNull()
  })
})
