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

import { calculateFinanceNetCashFlow, resolveFinanceRange } from './admin-finance-service.js'

test('resolveFinanceRange uses China date boundaries and defaults to today', () => {
  const result = resolveFinanceRange({}, '2026-09-02T01:00:00.000Z')
  assert.deepEqual(result, {
    from: '2026-09-01T16:00:00.000Z',
    to: '2026-09-02T16:00:00.000Z',
    fromDate: '2026-09-02',
    toDate: '2026-09-02',
    bucketFrom: '2026-09-01T16:00:00.000Z',
    bucketTo: '2026-09-02T16:00:00.000Z',
  })
})

test('resolveFinanceRange swaps reversed dates and keeps inclusive end date', () => {
  const result = resolveFinanceRange(
    { dateFrom: '2026-09-05', dateTo: '2026-09-03' },
    '2026-09-02T00:00:00.000Z',
  )
  assert.equal(result.fromDate, '2026-09-03')
  assert.equal(result.toDate, '2026-09-05')
  assert.equal(result.from, '2026-09-02T16:00:00.000Z')
  assert.equal(result.to, '2026-09-05T16:00:00.000Z')
})

test('resolveFinanceRange preserves minute precision while bucketing by China calendar day', () => {
  const result = resolveFinanceRange(
    { dateFrom: '2026-09-02 10:15', dateTo: '2026-09-02 18:30' },
    '2026-09-02T00:00:00.000Z',
  )
  assert.equal(result.from, '2026-09-02T02:15:00.000Z')
  assert.equal(result.to, '2026-09-02T10:30:59.000Z')
  assert.equal(result.bucketFrom, '2026-09-01T16:00:00.000Z')
  assert.equal(result.bucketTo, '2026-09-02T16:00:00.000Z')
})

test('calculateFinanceNetCashFlow uses cash basis and excludes reward accrual', () => {
  assert.equal(
    calculateFinanceNetCashFlow({
      paidAmount: 10000,
      refundAmount: 1000,
      rechargeAmount: 500,
      workerRecoveryAmount: 1000,
      withdrawAmount: 3000,
      afterSalesRefundAmount: 200,
    }),
    7300,
  )
})
