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

import {
  normalizeCloudtentaclesMatchName,
  resolveCloudtentaclesSkuByProductName,
} from './cloudtentacles-name-match-service.js'

test('normalizeCloudtentaclesMatchName normalizes punctuation and spaces', () => {
  assert.equal(
    normalizeCloudtentaclesMatchName(' 荣耀勋章礼包（30个） '),
    normalizeCloudtentaclesMatchName('荣耀勋章礼包(30个)'),
  )
})

test('resolveCloudtentaclesSkuByProductName matches SKU name with logged-in sources', async () => {
  const result = await resolveCloudtentaclesSkuByProductName('荣耀勋章礼包（30个）', {
    listCloudtentaclesSources: () => ({
      enabled: true,
      sources: [
        {
          key: 'account-a',
          label: '账号 A',
          enabled: true,
          baseUrl: 'https://cloud.example.com',
          deviceId: '-',
          deviceType: 0,
        },
      ],
    }),
    getCloudtentaclesSessionStateByKey: () => ({
      token: 'token-a',
      baseUrl: 'https://cloud.example.com',
      username: 'user-a',
      phone: '',
      loggedInAt: '2026-05-27T12:00:00.000Z',
      deviceId: '-',
      deviceType: 0,
    }),
    listCloudtentaclesSku: async () => ({
      baseUrl: 'https://cloud.example.com',
      itemCount: 1,
      rawItems: [],
      items: [
        {
          id: 74,
          name: '荣耀勋章礼包（30个）',
          inventory: 13837,
          price: 300,
        },
      ],
    }),
  })

  assert.equal(result?.cloudSkuId, 74)
  assert.equal(result?.cloudSkuName, '荣耀勋章礼包（30个）')
  assert.deepEqual(result?.cloudSourceKeys, ['account-a'])
  assert.equal(result?.matchMode, 'cloudtentacles_name')
})

test('resolveCloudtentaclesSkuByProductName 多账号匹配不提前固定账号', async () => {
  const result = await resolveCloudtentaclesSkuByProductName('商品A', {
    listCloudtentaclesSources: () => ({
      enabled: true,
      sources: [
        { key: 'account-a', enabled: true },
        { key: 'account-b', enabled: true },
      ],
    }),
    getCloudtentaclesSessionStateByKey: (key) => ({ token: `token-${key}` }),
    listCloudtentaclesSku: async () => ({
      items: [{ id: 28, name: '商品A', inventory: 10, price: 100 }],
    }),
  })

  assert.deepEqual(result?.cloudSourceKeys, ['account-a', 'account-b'])
  assert.equal(result?.resolvedSourceKey, '')
})

test('resolveCloudtentaclesSkuByProductName prefers override rule with multiple delivery items', async () => {
  const result = await resolveCloudtentaclesSkuByProductName('套装1', {
    listCloudtentaclesSources: () => ({
      enabled: true,
      sources: [
        {
          key: 'account-a',
          label: '账号 A',
          enabled: true,
          baseUrl: 'https://cloud.example.com',
          deviceId: '-',
          deviceType: 0,
        },
      ],
    }),
    getCloudtentaclesSessionStateByKey: () => ({
      token: 'token-a',
      baseUrl: 'https://cloud.example.com',
      username: 'user-a',
      phone: '',
      loggedInAt: '2026-05-27T12:00:00.000Z',
      deviceId: '-',
      deviceType: 0,
    }),
    listCloudtentaclesSku: async () => ({
      baseUrl: 'https://cloud.example.com',
      itemCount: 3,
      rawItems: [],
      items: [
        {
          id: 28,
          name: '商品A',
          inventory: 10,
          price: 360,
        },
        {
          id: 29,
          name: '商品B',
          inventory: 20,
          price: 888,
        },
        {
          id: 30,
          name: '套装1',
          inventory: 1,
          price: 999,
        },
      ],
    }),
    resolveCloudtentaclesOverrideRule: () => ({
      id: 'combo-1',
      enabled: true,
      productName: '套装1',
      normalizedProductName: normalizeCloudtentaclesMatchName('套装1'),
      sourceKey: '',
      deliveryItems: [
        {
          cloudSkuId: 28,
          cloudSkuName: '商品A',
          quantity: 1,
        },
        {
          cloudSkuId: 0,
          cloudSkuName: '商品B',
          quantity: 20,
        },
      ],
      notes: '',
    }),
  })

  assert.equal(result?.matchMode, 'cloudtentacles_override')
  assert.equal(result?.cloudSkuId, 28)
  assert.equal(result?.cloudSkuName, '商品A')
  assert.equal(result?.resolvedSourceKey, '')
  assert.deepEqual(result?.deliveryItems, [
    {
      cloudSkuId: 28,
      cloudSkuName: '商品A',
      quantity: 1,
    },
    {
      cloudSkuId: 29,
      cloudSkuName: '商品B',
      quantity: 20,
    },
  ])
})
