import test from 'node:test'
import assert from 'node:assert/strict'

import {
  assertAdminFulfillmentBindingsInput,
  buildAdminFulfillmentBindingUniqueKey,
  normalizeAdminFulfillmentBindingItem,
  validateAdminFulfillmentBindingConfigs,
} from './validation.js'

test('assertAdminFulfillmentBindingsInput rejects non-array payloads', () => {
  assert.doesNotThrow(() => assertAdminFulfillmentBindingsInput([]))
  assert.throws(() => assertAdminFulfillmentBindingsInput({}), /履约配置格式不正确/)
})

test('normalizeAdminFulfillmentBindingItem normalizes generic binding fields', () => {
  const result = normalizeAdminFulfillmentBindingItem(
    {
      provider: ' agiso ',
      platform: ' xianyu ',
      shopId: ' shop-1 ',
      shopName: ' 店铺 ',
      skuCode: 'sku-1',
      match: {
        externalSkuCode: 'ext-1',
      },
      config: {
        kuaishouShop: {
          note: 'keep',
        },
      },
    },
    { index: 0 },
  )

  assert.deepEqual(result, {
    provider: 'agiso',
    platform: 'xianyu',
    shopId: 'shop-1',
    shopName: '店铺',
    skuCode: 'sku-1',
    skuName: '',
    profileKey: 'manual_review',
    enabled: true,
    priority: undefined,
    config: {
      kuaishouShop: {
        note: 'keep',
      },
    },
    match: {
      externalSkuCode: 'ext-1',
      externalItemId: '',
      externalSkuName: '',
      config: {},
    },
  })
})

test('normalizeAdminFulfillmentBindingItem rejects missing sku and match conditions', () => {
  assert.throws(
    () =>
      normalizeAdminFulfillmentBindingItem(
        {
          provider: 'agiso',
          platform: 'xianyu',
          match: {
            externalSkuCode: 'ext-1',
          },
        },
        { index: 1 },
      ),
    /第 2 条规则缺少内部履约 SKU/,
  )

  assert.throws(
    () =>
      normalizeAdminFulfillmentBindingItem(
        {
          provider: 'agiso',
          platform: 'xianyu',
          skuCode: 'sku-1',
        },
        { index: 2 },
      ),
    /第 3 条规则至少需要一种外部匹配条件/,
  )
})

test('buildAdminFulfillmentBindingUniqueKey uses normalized external sku name and shop id rules', () => {
  assert.equal(
    buildAdminFulfillmentBindingUniqueKey({
      provider: '91kaquan',
      platform: 'kuaishou',
      shopId: 'shop-a',
      skuCode: 'sku-1',
      match: {
        externalItemId: '',
        externalSkuCode: '',
        externalSkuName: ' 礼包 A ',
      },
    }),
    '91kaquan::kuaishou::shop-a::::::礼包 a::sku-1',
  )
})

test('validateAdminFulfillmentBindingConfigs rejects missing fulfillment profile', async () => {
  await assert.rejects(
    () =>
      validateAdminFulfillmentBindingConfigs(
        [
          {
            provider: 'agiso',
            platform: 'xianyu',
            shopId: 'shop-1',
            skuCode: 'sku-1',
            profileKey: 'missing-profile',
            match: {
              externalSkuCode: 'ext-1',
            },
          },
        ],
        {
          kuaishouEticketSource: {},
          resolveKuaishouEticketShopConfig() {
            return null
          },
          async getFulfillmentProfileByKey() {
            return null
          },
        },
      ),
    /第 1 条规则使用了不存在的履约方式: missing-profile/,
  )
})

test('validateAdminFulfillmentBindingConfigs rejects duplicate rules after normalization', async () => {
  await assert.rejects(
    () =>
      validateAdminFulfillmentBindingConfigs(
        [
          {
            provider: 'agiso',
            platform: 'xianyu',
            shopId: 'shop-1',
            skuCode: 'sku-1',
            profileKey: 'manual_review',
            match: {
              externalSkuCode: ' ext-1 ',
            },
          },
          {
            provider: 'agiso',
            platform: 'xianyu',
            shopId: ' shop-1 ',
            skuCode: 'sku-1',
            profileKey: 'manual_review',
            match: {
              externalSkuCode: 'ext-1',
            },
          },
        ],
        {
          kuaishouEticketSource: {},
          resolveKuaishouEticketShopConfig() {
            return null
          },
          async getFulfillmentProfileByKey() {
            return { key: 'manual_review' }
          },
        },
      ),
    /第 2 条规则与其它规则重复/,
  )
})
