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

import {
  buildWorkOrderWhere,
  resolveOutstandingDepositAmount,
  shouldApplyAutomaticWorkerLevel,
} from './worker-platform/index.js'

test('resolveOutstandingDepositAmount deducts released and deducted history', () => {
  assert.equal(
    resolveOutstandingDepositAmount({
      frozen_amount: 20_000,
      released_amount: 5_000,
      deducted_amount: 3_000,
    }),
    12_000,
  )
})

test('resolveOutstandingDepositAmount never returns a negative deposit', () => {
  assert.equal(
    resolveOutstandingDepositAmount({
      frozen_amount: 10_000,
      released_amount: 12_000,
      deducted_amount: 1_000,
    }),
    0,
  )
  assert.equal(resolveOutstandingDepositAmount(null), 0)
})

test('automatic worker level changes only move to a higher threshold', () => {
  assert.equal(
    shouldApplyAutomaticWorkerLevel({
      currentLevelId: 5,
      currentThreshold: 100,
      candidateLevelId: 1,
      candidateThreshold: 0,
    }),
    false,
  )
  assert.equal(
    shouldApplyAutomaticWorkerLevel({
      currentLevelId: 1,
      currentThreshold: 0,
      candidateLevelId: 2,
      candidateThreshold: 10,
    }),
    true,
  )
  assert.equal(
    shouldApplyAutomaticWorkerLevel({
      currentLevelId: 2,
      currentThreshold: 10,
      candidateLevelId: 3,
      candidateThreshold: 10,
    }),
    false,
  )
})

test('指定打手的运营状态统计保留可见范围和拼单汇总关联', () => {
  const query = buildWorkOrderWhere({
    workerSharingId: 3,
    useOperationalStatus: true,
  })

  assert.match(query.joins, /worker_visible/)
  assert.match(query.joins, /operational_share_summary/)
  assert.match(query.joins, /LEFT JOIN LATERAL/)
  assert.ok(
    query.joins.indexOf('worker_visible') < query.joins.indexOf('operational_share_summary'),
  )
  assert.deepEqual(query.params, [3])
})

test('未指定打手的运营状态统计使用一次全表拼单汇总', () => {
  const query = buildWorkOrderWhere({ useOperationalStatus: true })

  assert.match(query.joins, /GROUP BY wos\.work_order_id/)
  assert.match(
    query.joins,
    /operational_share_summary ON operational_share_summary\.work_order_id = wo\.id/,
  )
  assert.doesNotMatch(query.joins, /worker_visible/)
})

test('大厅容量先确定全局候选集合，再应用页面筛选条件', () => {
  const query = buildWorkOrderWhere({
    statuses: ['open'],
    keyword: '测试商品',
    categoryId: 3,
    hallCandidateLimit: 50,
    visibleAfterIso: '2026-08-21T00:00:00.000Z',
    excludeFilledSharing: true,
  })

  assert.match(query.whereClause, /candidate\.status = 'open'/)
  assert.match(query.whereClause, /ORDER BY candidate\.hall_queued_at ASC NULLS LAST/)
  assert.match(query.whereClause, /LIMIT \$4/)
  const candidateSql = query.whereClause.slice(query.whereClause.indexOf('candidate.status'))
  assert.doesNotMatch(candidateSql, /candidate\.category_id|candidate.*测试商品/)
  assert.deepEqual(query.params, [['open'], '%测试商品%', 3, 50, '{}', '2026-08-21T00:00:00.000Z'])
})

test('后台接单工单支持按接单分类筛选', () => {
  const query = buildWorkOrderWhere({ categoryId: 7 })
  assert.match(query.whereClause, /wo\.category_id = \$1/)
  assert.deepEqual(query.params, [7])
})

test('空白搜索词不生成全表 LIKE 条件', () => {
  const query = buildWorkOrderWhere({ keyword: '   ' })
  assert.doesNotMatch(query.whereClause, /LIKE LOWER/)
  assert.deepEqual(query.params, [])
})

test('大厅候选按分类上限取队首后再受全局上限限制', () => {
  const query = buildWorkOrderWhere({
    hallCandidateLimit: 20,
    hallCategoryLimits: { '1': 3, '2': 5 },
  })
  assert.match(query.whereClause, /ROW_NUMBER\(\) OVER/)
  assert.match(query.whereClause, /category_rank/)
  assert.deepEqual(query.params, [20, '{"1":3,"2":5}'])
})
