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

import {
  appendUidToUrl,
  assertLewanAutoFulfillmentUidReady,
  assertValidClaimUid,
  buildClaimIdentityAdminSummary,
  isClaimUidMatched,
  normalizeClaimUid,
} from './claim-identity.js'

test('normalizeClaimUid trims and removes spaces', () => {
  assert.equal(normalizeClaimUid('  166 909 256 '), '166909256')
})

test('assertValidClaimUid accepts common game uid', () => {
  assert.equal(assertValidClaimUid('166909256'), '166909256')
})

test('assertValidClaimUid rejects empty value', () => {
  assert.throws(() => assertValidClaimUid(''), /请输入游戏 UID/)
})

test('assertValidClaimUid rejects non-digit uid', () => {
  assert.throws(() => assertValidClaimUid('abc123'), /仅支持纯数字/)
  assert.throws(() => assertValidClaimUid('166909-256'), /仅支持纯数字/)
  assert.throws(() => assertValidClaimUid('166909_256'), /仅支持纯数字/)
})

test('assertValidClaimUid rejects 91 order no (16 digits) as uid', () => {
  // 91 单号 2621801687456831 为 16 位纯数字，超出 UID 上限，防止误填
  assert.throws(() => assertValidClaimUid('2621801687456831'), /长度不能超过 15/)
})

test('assertValidClaimUid accepts uid within 15 digits', () => {
  assert.equal(assertValidClaimUid('123456789012345'), '123456789012345')
})

test('isClaimUidMatched compares normalized values', () => {
  assert.equal(isClaimUidMatched('166909256', '166909256'), true)
  assert.equal(isClaimUidMatched('166909256', '999'), false)
})

test('appendUidToUrl sets uid query param', () => {
  const source =
    'http://skin-exchange.yiquyou.icu/h5/bind?code=eyJpdi&product_name=%E5%A5%97%E8%A3%85'
  const next = appendUidToUrl(source, '166909256')
  const url = new URL(next)
  assert.equal(url.searchParams.get('uid'), '166909256')
  assert.equal(url.searchParams.get('code'), 'eyJpdi')
})

test('appendUidToUrl overwrites existing uid', () => {
  const next = appendUidToUrl('https://example.com/h5/bind?code=abc&uid=old', 'new-uid')
  assert.equal(new URL(next).searchParams.get('uid'), 'new-uid')
})

test('assertLewanAutoFulfillmentUidReady rejects legacy task without uid', () => {
  assert.throws(
    () =>
      assertLewanAutoFulfillmentUidReady({
        context_json: JSON.stringify({ kuaishouCloudFulfillment: {} }),
      }),
    /尚未填写游戏 UID/,
  )
})

test('assertLewanAutoFulfillmentUidReady allows mock without uid', () => {
  assert.equal(
    assertLewanAutoFulfillmentUidReady({
      context_json: JSON.stringify({
        kuaishouCloudFulfillment: { mock: { enabled: true } },
      }),
    }),
    '',
  )
})

test('assertLewanAutoFulfillmentUidReady accepts submitted uid', () => {
  assert.equal(
    assertLewanAutoFulfillmentUidReady({
      context_json: JSON.stringify({
        claimIdentity: { expectedUid: '10001', submittedAt: '2026-07-10T00:00:00.000Z' },
        kuaishouCloudFulfillment: {},
      }),
    }),
    '10001',
  )
})

test('buildClaimIdentityAdminSummary reports match state', () => {
  const summary = buildClaimIdentityAdminSummary(
    {
      claimIdentity: { expectedUid: '10001', submittedAt: 't', source: 'claim_page' },
    },
    {
      flowLike: {
        binding: { roleId: '10001', roleName: '测试角色' },
        role: { rid: '10001', name: '测试角色' },
      },
    },
  )
  assert.equal(summary.ready, true)
  assert.equal(summary.uidMatched, true)
  assert.equal(summary.compatibilityMode, 'uid')
  assert.equal(summary.boundUid, '10001')
  assert.equal(summary.displaySource, 'live')
})

test('buildClaimIdentityAdminSummary marks legacy without uid', () => {
  const summary = buildClaimIdentityAdminSummary({}, { flowLike: { binding: {} } })
  assert.equal(summary.ready, false)
  assert.equal(summary.compatibilityMode, 'legacy_no_uid')
  assert.equal(summary.uidMatched, null)
  assert.equal(summary.displaySource, 'live')
})

test('buildClaimIdentityAdminSummary prefers shipped snapshot after dispatch pollution', () => {
  const summary = buildClaimIdentityAdminSummary(
    {
      claimIdentity: { expectedUid: '3781015014', submittedAt: 't', source: 'claim_page' },
    },
    {
      flowLike: {
        dispatch: { status: 'success', dispatchAt: '2026-07-16T06:25:13.000Z' },
        shipped: {
          expectedUid: '3781015014',
          roleId: '3781015014',
          roleName: 'l迈',
          vnId: 189363,
          vnPhone: '18641685924',
          dispatchedAt: '2026-07-16T06:25:13.000Z',
        },
        // 发货后换号污染：当前绑定已变成别的角色
        binding: { roleId: '3805904834', roleName: '他爆炸' },
        role: { rid: '3805904834', name: '他爆炸' },
      },
    },
  )
  assert.equal(summary.displaySource, 'shipped')
  assert.equal(summary.boundUid, '3781015014')
  assert.equal(summary.boundRoleName, 'l迈')
  assert.equal(summary.uidMatched, true)
  assert.equal(summary.shippedUid, '3781015014')
  assert.match(summary.note, /已发货/)
  assert.match(summary.note, /当前虚拟号角色已变化/)
})
