import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
import { fileURLToPath } from 'node:url'

import { isArchivedExpenseClaim } from '../src/utils/expenseClaimArchive.js'

test('isArchivedExpenseClaim recognizes finance archive stage', () => {
  assert.equal(
    isArchivedExpenseClaim({ status: 'approved', approval_stage: '归档入账' }),
    true
  )
  assert.equal(
    isArchivedExpenseClaim({
      status: 'approved',
      approval_stage: '审批完成',
      claim_no: 'AP-20260525120000-ABCDEFGH',
      expense_type: 'travel_application'
    }),
    false
  )
  assert.equal(
    isArchivedExpenseClaim({
      status: 'approved',
      approval_stage: '申请归档',
      claim_no: 'AP-20260525120000-ABCDEFGH',
      expense_type: 'travel_application'
    }),
    true
  )
  assert.equal(
    isArchivedExpenseClaim({ status: 'paid', approval_stage: '已付款' }),
    true
  )
})

test('isArchivedExpenseClaim ignores in-progress claims', () => {
  assert.equal(
    isArchivedExpenseClaim({ status: 'submitted', approval_stage: '财务审批' }),
    false
  )
  assert.equal(
    isArchivedExpenseClaim({ status: 'pending_payment', approval_stage: '待付款' }),
    false
  )
})

test('archive data stays available through api client but archive center is removed from navigation', () => {
  const navigationScript = readFileSync(
    fileURLToPath(new URL('../src/composables/useNavigation.js', import.meta.url)),
    'utf8'
  )
  const reimbursementsService = readFileSync(
    fileURLToPath(new URL('../src/services/reimbursements.js', import.meta.url)),
    'utf8'
  )

  assert.doesNotMatch(navigationScript, /id:\s*'archive'/)
  assert.match(reimbursementsService, /\/reimbursements\/claims\/archives/)
})

test('archive center uses generic archive category and type wording', () => {
  const archiveView = readFileSync(
    fileURLToPath(new URL('../src/views/ArchiveCenterView.vue', import.meta.url)),
    'utf8'
  )
  const archiveScript = readFileSync(
    fileURLToPath(new URL('../src/views/scripts/ArchiveCenterView.js', import.meta.url)),
    'utf8'
  )

  assert.match(archiveScript, /const tabs = \[ARCHIVE_TAB_ALL, ARCHIVE_TAB_APPLICATION, ARCHIVE_TAB_REIMBURSEMENT\]/)
  assert.match(archiveScript, /const ARCHIVE_TAB_APPLICATION = '申请归档'/)
  assert.match(archiveScript, /const ARCHIVE_TAB_REIMBURSEMENT = '报销归档'/)
  assert.match(archiveScript, /archiveType:\s*isApplicationDocument \? ARCHIVE_TYPE_APPLICATION : ARCHIVE_TYPE_REIMBURSEMENT/)
  assert.match(archiveScript, /archiveTab:\s*isApplicationDocument \? ARCHIVE_TAB_APPLICATION : ARCHIVE_TAB_REIMBURSEMENT/)
  assert.match(archiveScript, /const ARCHIVE_TYPE_REIMBURSEMENT = '报销'/)
  assert.match(archiveScript, /const ARCHIVE_TYPE_APPLICATION = '申请'/)
  assert.doesNotMatch(archiveScript, /'差旅报销'/)
  assert.doesNotMatch(archiveScript, /'招待报销'/)
  assert.doesNotMatch(archiveScript, /'其他费用'/)
  assert.match(archiveView, /placeholder="搜索单号、申请人、部门、归档类型\.\.\."/)
  assert.match(archiveView, /<th>归档类型<\/th>/)
  assert.match(archiveView, /\{\{\s*row\.archiveType\s*\}\}/)
  assert.doesNotMatch(archiveView, /<th>报销类型<\/th>/)
})
