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

import {
  canReuseAffiliateDashBindSnapshot,
  isAffiliateDashBindSnapshotStale,
  shouldRefreshAffiliateDashBindStateForSnapshot,
} from './kuaishou-cloud-claim-service.js'

test('isAffiliateDashBindSnapshotStale detects bind uuid that belongs to old uid', () => {
  assert.equal(
    isAffiliateDashBindSnapshotStale(
      {
        bindUuid: 'bind-old',
        bindUrl: 'https://dash.example/bind-old',
        expectedGameAccount: '111',
        gameAccount: '111',
      },
      '222',
    ),
    true,
  )
})

test('isAffiliateDashBindSnapshotStale treats unknown bind uid as stale', () => {
  assert.equal(
    isAffiliateDashBindSnapshotStale(
      {
        bindUuid: 'bind-unknown',
        bindUrl: 'https://dash.example/bind-unknown',
      },
      '222',
    ),
    true,
  )
})

test('isAffiliateDashBindSnapshotStale keeps current uid bind snapshot', () => {
  assert.equal(
    isAffiliateDashBindSnapshotStale(
      {
        bindUuid: 'bind-current',
        bindUrl: 'https://dash.example/bind-current',
        expectedGameAccount: '222',
        gameAccount: '222',
      },
      '222',
    ),
    false,
  )
})

test('canReuseAffiliateDashBindSnapshot reuses pending current uid bind', () => {
  assert.equal(
    canReuseAffiliateDashBindSnapshot(
      {
        bindUuid: 'bind-current',
        bindUrl: 'https://dash.example/bind-current',
        expectedGameAccount: '222',
        gameAccount: '222',
        bound: false,
        bindMismatch: false,
      },
      '222',
    ),
    true,
  )
})

test('canReuseAffiliateDashBindSnapshot refuses old uid bind', () => {
  assert.equal(
    canReuseAffiliateDashBindSnapshot(
      {
        bindUuid: 'bind-old',
        bindUrl: 'https://dash.example/bind-old',
        expectedGameAccount: '111',
        gameAccount: '111',
        bound: false,
        bindMismatch: false,
      },
      '222',
    ),
    false,
  )
})

test('canReuseAffiliateDashBindSnapshot refuses bound snapshot without bound account', () => {
  assert.equal(
    canReuseAffiliateDashBindSnapshot(
      {
        bindUuid: 'bind-current',
        bindUrl: 'https://dash.example/bind-current',
        expectedGameAccount: '222',
        gameAccount: '222',
        bound: true,
        boundAccount: '',
        bindMismatch: false,
      },
      '222',
    ),
    false,
  )
})

test('canReuseAffiliateDashBindSnapshot reuses verified matching bound account', () => {
  assert.equal(
    canReuseAffiliateDashBindSnapshot(
      {
        bindUuid: 'bind-current',
        bindUrl: 'https://dash.example/bind-current',
        expectedGameAccount: '222',
        gameAccount: '222',
        bound: true,
        boundAccount: '222',
        bindMismatch: false,
      },
      '222',
    ),
    true,
  )
})

test('canReuseAffiliateDashBindSnapshot refuses mismatched bound account', () => {
  assert.equal(
    canReuseAffiliateDashBindSnapshot(
      {
        bindUuid: 'bind-current',
        bindUrl: 'https://dash.example/bind-current',
        expectedGameAccount: '222',
        gameAccount: '222',
        bound: true,
        boundAccount: '333',
        bindMismatch: false,
      },
      '222',
    ),
    false,
  )
})

test('shouldRefreshAffiliateDashBindStateForSnapshot keeps polling before submit', () => {
  assert.equal(
    shouldRefreshAffiliateDashBindStateForSnapshot({
      taskStatus: 'waiting_binding',
      orderStatus: 'paid',
      submitStatus: '',
    }),
    true,
  )
})

test('shouldRefreshAffiliateDashBindStateForSnapshot stops after submit starts', () => {
  assert.equal(
    shouldRefreshAffiliateDashBindStateForSnapshot({
      taskStatus: 'redeeming',
      orderStatus: 'delivering',
      submitStatus: 'delivering',
    }),
    false,
  )
})

test('shouldRefreshAffiliateDashBindStateForSnapshot stops after delivered', () => {
  assert.equal(
    shouldRefreshAffiliateDashBindStateForSnapshot({
      taskStatus: 'redeemed',
      orderStatus: 'delivered',
      submitStatus: 'delivered',
    }),
    false,
  )
})

test('shouldRefreshAffiliateDashBindStateForSnapshot stops for failed platform result', () => {
  assert.equal(
    shouldRefreshAffiliateDashBindStateForSnapshot({
      taskStatus: 'retry_pending',
      orderStatus: 'ship_failed',
      submitStatus: '',
    }),
    false,
  )
})
