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

import {
  isWorkOrderGiftDepositForfeitable,
  normalizeWorkOrderAcceptanceMode,
  normalizeWorkOrderGiftCooldownHours,
  normalizeWorkOrderGiftCooldownMinutes,
  resolveWorkOrderGiftPhase,
  WORK_ORDER_ACCEPTANCE_MODE,
  WORK_ORDER_GIFT_PHASE,
} from './work-order-acceptance-mode.js'

test('好友赠送押金不可退判定收敛确认加好友标记', () => {
  assert.equal(
    isWorkOrderGiftDepositForfeitable({
      acceptance_mode: 'friend_gift',
      friend_added_at: '2026-08-01T00:00:00.000Z',
    }),
    true,
  )
  // 未确认加好友（待交资料/已交资料阶段）：押金仍可退
  assert.equal(
    isWorkOrderGiftDepositForfeitable({
      acceptance_mode: 'friend_gift',
      friend_added_at: null,
    }),
    false,
  )
  // 标准模式不受影响
  assert.equal(
    isWorkOrderGiftDepositForfeitable({
      acceptance_mode: 'standard',
      friend_added_at: '2026-08-01T00:00:00.000Z',
    }),
    false,
  )
  assert.equal(isWorkOrderGiftDepositForfeitable({}), false)
})

test('赠送阶段按冷却到点实时推导 gift_ready', () => {
  const past = new Date(Date.now() - 60_000).toISOString()
  const future = new Date(Date.now() + 60_000).toISOString()
  assert.equal(
    resolveWorkOrderGiftPhase({
      acceptance_mode: WORK_ORDER_ACCEPTANCE_MODE.FRIEND_GIFT,
      gift_phase: WORK_ORDER_GIFT_PHASE.FRIEND_COUNTDOWN,
      gift_available_at: past,
    }),
    WORK_ORDER_GIFT_PHASE.GIFT_READY,
  )
  assert.equal(
    resolveWorkOrderGiftPhase({
      acceptance_mode: WORK_ORDER_ACCEPTANCE_MODE.FRIEND_GIFT,
      gift_phase: WORK_ORDER_GIFT_PHASE.FRIEND_COUNTDOWN,
      gift_available_at: future,
    }),
    WORK_ORDER_GIFT_PHASE.FRIEND_COUNTDOWN,
  )
  assert.equal(normalizeWorkOrderAcceptanceMode('friend_gift'), 'friend_gift')
  assert.equal(normalizeWorkOrderAcceptanceMode(''), 'standard')
})

test('好友赠送按分钟冷却支持最长三十天并忽略负数', () => {
  assert.equal(normalizeWorkOrderGiftCooldownHours(0), 0)
  assert.equal(normalizeWorkOrderGiftCooldownMinutes(15), 15)
  assert.equal(normalizeWorkOrderGiftCooldownMinutes(43_201), 43_200)
  assert.equal(normalizeWorkOrderGiftCooldownMinutes(-1), 0)
})
