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

import { resolveWithdrawApprovalPlan, resolveWithdrawRejectionPlan } from './worker-finance-repo.js'

test('提现通过(新口径):申请时已冻结,确认打款只解冻不动可用余额', () => {
  const plan = resolveWithdrawApprovalPlan({
    requestFrozenAmount: 8_000,
    frozenWithdrawAmount: 8_000,
    availableAmount: 2_000,
    amount: 8_000,
  })
  assert.deepEqual(plan, {
    ok: true,
    mode: 'frozen',
    nextAvailableAmount: 2_000,
    nextFrozenWithdrawAmount: 0,
    ledgerAmount: 0,
  })
})

test('提现通过(新口径):多笔待审时只解冻当前申请的冻结额度', () => {
  const plan = resolveWithdrawApprovalPlan({
    requestFrozenAmount: 3_000,
    frozenWithdrawAmount: 11_000,
    availableAmount: 5_000,
    amount: 3_000,
  })
  assert.deepEqual(plan, {
    ok: true,
    mode: 'frozen',
    nextAvailableAmount: 5_000,
    nextFrozenWithdrawAmount: 8_000,
    ledgerAmount: 0,
  })
})

test('提现通过(新口径):冻结额度异常不足时拒绝并保持现状', () => {
  const plan = resolveWithdrawApprovalPlan({
    requestFrozenAmount: 8_000,
    frozenWithdrawAmount: 1_000,
    availableAmount: 9_000,
    amount: 8_000,
  })
  assert.deepEqual(plan, { ok: false, reason: 'withdraw_insufficient' })
})

test('提现通过(旧数据兜底):未冻结申请沿用确认时扣款口径', () => {
  const plan = resolveWithdrawApprovalPlan({
    requestFrozenAmount: 0,
    frozenWithdrawAmount: 0,
    availableAmount: 10_000,
    amount: 8_000,
  })
  assert.deepEqual(plan, {
    ok: true,
    mode: 'legacy',
    nextAvailableAmount: 2_000,
    nextFrozenWithdrawAmount: 0,
    ledgerAmount: -8_000,
  })
})

test('提现通过(旧数据兜底):可用余额不足时拒绝', () => {
  const plan = resolveWithdrawApprovalPlan({
    requestFrozenAmount: 0,
    frozenWithdrawAmount: 0,
    availableAmount: 5_000,
    amount: 8_000,
  })
  assert.deepEqual(plan, { ok: false, reason: 'withdraw_insufficient' })
})

test('提现拒绝(新口径):冻结资金全额退回可用余额', () => {
  const plan = resolveWithdrawRejectionPlan({
    requestFrozenAmount: 8_000,
    frozenWithdrawAmount: 8_000,
    availableAmount: 2_000,
  })
  assert.deepEqual(plan, {
    ok: true,
    releaseAmount: 8_000,
    nextAvailableAmount: 10_000,
    nextFrozenWithdrawAmount: 0,
  })
})

test('提现拒绝(新口径):多笔待审时只退回当前申请额度', () => {
  const plan = resolveWithdrawRejectionPlan({
    requestFrozenAmount: 3_000,
    frozenWithdrawAmount: 11_000,
    availableAmount: 4_000,
  })
  assert.deepEqual(plan, {
    ok: true,
    releaseAmount: 3_000,
    nextAvailableAmount: 7_000,
    nextFrozenWithdrawAmount: 8_000,
  })
})

test('提现拒绝(旧数据):未冻结申请不退回资金', () => {
  const plan = resolveWithdrawRejectionPlan({
    requestFrozenAmount: 0,
    frozenWithdrawAmount: 0,
    availableAmount: 2_000,
  })
  assert.deepEqual(plan, {
    ok: true,
    releaseAmount: 0,
    nextAvailableAmount: 2_000,
    nextFrozenWithdrawAmount: 0,
  })
})

test('提现拒绝: 冻结额度异常不足时保持申请待处理', () => {
  const plan = resolveWithdrawRejectionPlan({
    requestFrozenAmount: 8_000,
    frozenWithdrawAmount: 1_000,
    availableAmount: 9_000,
  })
  assert.deepEqual(plan, { ok: false, reason: 'withdraw_insufficient' })
})
