import { mount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest'
import { uiAdapterKey } from '../../adapter/contracts'
import { nativeUiAdapter } from '../../adapter/native'
import KsMoneyField from '../KsMoneyField.vue'
import KsQuantityField from '../KsQuantityField.vue'

const globalProvide = {
  global: {
    provide: { [uiAdapterKey as unknown as string]: nativeUiAdapter },
    stubs: {
      InputNumber: {
        props: ['modelValue', 'inputId'],
        template: '<input :id="inputId" :value="modelValue" @input="$emit(\'update:modelValue\', Number($event.target.value))" />',
      },
    },
  },
}

describe('KsMoneyField', () => {
  it('connects the label, shows the currency code, and emits numeric updates', async () => {
    const wrapper = mount(KsMoneyField, { props: { modelValue: 1250000, label: '단가' }, ...globalProvide })
    expect(wrapper.get('label').text()).toContain('단가')
    expect(wrapper.text()).toContain('KRW')
    await wrapper.get('input').setValue('2000000')
    expect(wrapper.emitted('update:modelValue')?.[0]).toEqual([2000000])
  })

  it('accepts a non-KRW currency code', () => {
    const wrapper = mount(KsMoneyField, { props: { modelValue: 100, label: '금액', currency: 'USD' }, ...globalProvide })
    expect(wrapper.text()).toContain('USD')
  })
})

describe('KsQuantityField', () => {
  it('shows the available quantity as help text when under the limit', () => {
    const wrapper = mount(KsQuantityField, { props: { modelValue: 5, label: '주문수량', unit: 'EA', availableQuantity: 8 }, ...globalProvide })
    expect(wrapper.text()).toContain('가용 8 EA')
    expect(wrapper.find('[role="alert"]').exists()).toBe(false)
  })

  it('raises an accessible error when the value exceeds the available quantity', () => {
    const wrapper = mount(KsQuantityField, { props: { modelValue: 10, label: '주문수량', unit: 'EA', availableQuantity: 8 }, ...globalProvide })
    expect(wrapper.get('[role="alert"]').text()).toContain('가용 수량(8 EA)을 초과했습니다.')
  })
})
