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

import {
  applyOptionalStringField,
  findMatchingObservedBinding,
  mapAdminAgisoMessagingDefaults,
  mapAdminAgisoShopConfigItem,
  mapAdminFulfillmentBindingConfigItem,
  mapAdminObservedProductItem,
  matchesObservedProduct,
} from './domain.js'

test('mapAdminAgisoMessagingDefaults normalizes escaped newlines', () => {
  assert.deepEqual(
    mapAdminAgisoMessagingDefaults({
      messageTemplate: '  hello\\nworld  ',
      autoDeliveryMessageTemplate: ' done\\nnow ',
    }),
    {
      messageTemplate: 'hello\nworld',
      autoDeliveryMessageTemplate: 'done\nnow',
    },
  )
})

test('mapAdminAgisoShopConfigItem masks secrets and reports configured flags', () => {
  assert.deepEqual(
    mapAdminAgisoShopConfigItem('1001', {
      shopName: ' 店铺A ',
      accessToken: 'abcdef1234567890',
      enabled: true,
      messageTemplate: ' hi\\nall ',
      autoDeliveryMessageTemplate: ' ok ',
      appSecret: 'secret-1',
      apiVersion: ' v2 ',
      sendMessageEndpoint: ' /send ',
    }),
    {
      shopId: '1001',
      shopName: '店铺A',
      accessToken: 'abcdef1234567890',
      accessTokenMasked: 'abcdef****567890',
      enabled: true,
      messageTemplate: 'hi\nall',
      autoDeliveryMessageTemplate: 'ok',
      appSecretConfigured: true,
      apiVersion: 'v2',
      sendMessageEndpoint: '/send',
    },
  )
})

test('applyOptionalStringField updates and clears normalized template fields', () => {
  const target = { messageTemplate: 'old' }
  applyOptionalStringField(target, 'messageTemplate', { messageTemplate: ' next\\nline ' })
  assert.deepEqual(target, { messageTemplate: 'next\nline' })

  applyOptionalStringField(target, 'messageTemplate', { messageTemplate: '   ' })
  assert.deepEqual(target, {})
})

test('mapAdminFulfillmentBindingConfigItem normalizes binding shape', () => {
  assert.deepEqual(
    mapAdminFulfillmentBindingConfigItem({
      provider: ' agiso ',
      platform: ' xianyu ',
      shopId: ' shop-1 ',
      skuCode: ' sku-1 ',
      priority: '80',
      match: {
        externalSkuCode: ' ext-1 ',
      },
    }),
    {
      provider: 'agiso',
      platform: 'xianyu',
      shopId: 'shop-1',
      shopName: '',
      skuCode: 'sku-1',
      skuName: '',
      profileKey: '',
      enabled: true,
      priority: 80,
      config: {},
      match: {
        externalSkuCode: 'ext-1',
        externalItemId: '',
        externalSkuName: '',
        config: {},
      },
    },
  )
})

test('matchesObservedProduct honors provider platform shop and external fields', () => {
  const binding = {
    provider: 'agiso',
    platform: 'xianyu',
    shopId: 'shop-1',
    match: {
      externalSkuCode: 'sku-ext-1',
      externalItemId: '',
      externalSkuName: '',
    },
  }

  assert.equal(
    matchesObservedProduct(binding, {
      provider: 'agiso',
      platform: 'xianyu',
      shopId: 'shop-1',
      externalSkuCode: 'sku-ext-1',
    }),
    true,
  )

  assert.equal(
    matchesObservedProduct(binding, {
      provider: 'agiso',
      platform: 'xianyu',
      shopId: 'shop-2',
      externalSkuCode: 'sku-ext-1',
    }),
    false,
  )
})

test('mapAdminObservedProductItem attaches matched binding summary', () => {
  const bindings = [
    {
      provider: 'agiso',
      platform: 'xianyu',
      shopId: 'shop-1',
      skuCode: 'sku-1',
      skuName: 'SKU 1',
      profileKey: 'profile-1',
      match: {
        externalSkuCode: 'ext-1',
      },
    },
  ]
  const observed = {
    provider: 'agiso',
    platform: 'xianyu',
    shopId: 'shop-1',
    shopName: '店铺1',
    externalItemId: '',
    externalSkuCode: 'ext-1',
    externalSkuName: '礼包1',
    latestSeenAt: '2026-05-04T10:00:00.000Z',
    orderItemCount: 2,
  }

  assert.deepEqual(findMatchingObservedBinding(bindings, observed), bindings[0])
  assert.deepEqual(mapAdminObservedProductItem(observed, bindings), {
    provider: 'agiso',
    platform: 'xianyu',
    shopId: 'shop-1',
    shopName: '店铺1',
    externalItemId: '',
    externalSkuCode: 'ext-1',
    externalSkuName: '礼包1',
    latestSeenAt: '2026-05-04T10:00:00.000Z',
    orderItemCount: 2,
    configured: true,
    matchedBinding: {
      skuCode: 'sku-1',
      skuName: 'SKU 1',
      profileKey: 'profile-1',
    },
  })
})
