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

import {
  resolveHallDepositAmount,
  resolveHallProductName,
  resolveHallRemainingQuantity,
  resolveHallRewardAmount,
} from '../src/utils/worker-product-name.ts'

test('拼单大厅标题按剩余份数更新阿拉伯数字规格', () => {
  assert.equal(
    resolveHallProductName({
      productName: '指挥官密钥15个',
      sharing: { enabled: true, totalQuantity: 15 },
      sharingProgress: { joinedQuantity: 3 },
    }),
    '指挥官密钥12个',
  )
})

test('拼单大厅标题兼容中文数字规格', () => {
  assert.equal(
    resolveHallProductName({
      productName: '指挥官密钥十五份',
      sharing: { enabled: true, totalQuantity: 15 },
      sharingProgress: { joinedQuantity: 4 },
    }),
    '指挥官密钥11份',
  )
})

test('普通单保留原始标题', () => {
  assert.equal(
    resolveHallProductName({
      productName: '指挥官密钥1个',
      sharing: { enabled: false, totalQuantity: 1 },
      sharingProgress: { joinedQuantity: 0 },
    }),
    '指挥官密钥1个',
  )
})

test('立即抢单的份数、报酬和保证金都按剩余数量计算', () => {
  const order = {
    productName: '指挥官密钥15个',
    rewardAmount: 10_500,
    freezeDepositAmount: 1_500,
    sharing: { enabled: true, totalQuantity: 15, unitReward: 700 },
    sharingProgress: { joinedQuantity: 3 },
  }

  assert.equal(resolveHallRemainingQuantity(order), 12)
  assert.equal(resolveHallRewardAmount(order), 8_400)
  assert.equal(resolveHallDepositAmount(order), 1_200)
})

test('拼单大厅保证金先按个人份额计算再抵扣免押额度', () => {
  assert.equal(
    resolveHallDepositAmount({
      freezeDepositAmount: 7_000,
      depositFreeAmount: 3_000,
      sharing: { enabled: true, totalQuantity: 10 },
      sharingProgress: { joinedQuantity: 5 },
    }),
    2_000,
  )
})

test('拼单份数用完后剩余报酬和保证金归零', () => {
  const order = {
    rewardAmount: 10_500,
    freezeDepositAmount: 1_500,
    sharing: { enabled: true, totalQuantity: 15, unitReward: 700 },
    sharingProgress: { joinedQuantity: 15 },
  }

  assert.equal(resolveHallRemainingQuantity(order), 0)
  assert.equal(resolveHallRewardAmount(order), 0)
  assert.equal(resolveHallDepositAmount(order), 0)
})
