import assert from 'node:assert/strict'

import {
  RECEIPT_FILTER_ALL,
  applyReceiptListFilters,
  buildReceiptFilterControls,
  buildReceiptFilterTokens
} from '../src/views/scripts/receiptFolderListFilters.js'

const rows = [
  {
    id: 'r-1',
    document_type: 'train_ticket',
    document_type_label: '火车票',
    scene_code: 'travel',
    scene_label: '差旅',
    document_date: '2026-05-02',
    avg_score: 0.96
  },
  {
    id: 'r-2',
    document_type: 'vat_invoice',
    document_type_label: '增值税发票',
    scene_code: 'office',
    scene_label: '办公',
    uploaded_at: '2026-04-18T10:00:00Z',
    avg_score: 0.82
  },
  {
    id: 'r-3',
    document_type: 'vat_invoice',
    document_type_label: '增值税发票',
    scene_code: 'travel',
    scene_label: '差旅',
    document_date: '',
    uploaded_at: '2026-05-20T08:00:00Z',
    avg_score: 0
  }
]

function testBuildsDynamicOptions() {
  const controls = buildReceiptFilterControls(rows, {})
  const typeControl = controls.find((item) => item.key === 'documentType')
  const monthControl = controls.find((item) => item.key === 'month')

  assert.equal(typeControl.options[0].value, RECEIPT_FILTER_ALL)
  assert.deepEqual(typeControl.options.map((item) => item.value).sort(), ['all', 'train_ticket', 'vat_invoice'])
  assert.deepEqual(monthControl.options.map((item) => item.value), ['all', '2026-05', '2026-04'])
}

function testAppliesCombinedFilters() {
  const result = applyReceiptListFilters(rows, {
    documentType: 'vat_invoice',
    scene: 'travel',
    month: '2026-05',
    quality: 'missing'
  })

  assert.deepEqual(result.map((item) => item.id), ['r-3'])
}

function testBuildsReadableTokens() {
  const filters = {
    documentType: 'train_ticket',
    scene: 'travel',
    month: RECEIPT_FILTER_ALL,
    quality: 'high'
  }
  const tokens = buildReceiptFilterTokens(buildReceiptFilterControls(rows, filters), filters)

  assert.deepEqual(tokens, ['票据类型：火车票', '费用场景：差旅', '置信度：高置信度'])
}

function run() {
  testBuildsDynamicOptions()
  testAppliesCombinedFilters()
  testBuildsReadableTokens()
  console.log('receipt folder list filter tests passed')
}

run()
