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

import { isArchivedRequestView, normalizeRequestForUi } from '../src/utils/requestViewModel.js'

test('normalizes backend approval_stage for in-progress claim details', () => {
  const request = normalizeRequestForUi({
    id: 'EXP-202605-001',
    claim_id: 'claim-1',
    status: 'submitted',
    approval_stage: '直属领导审批',
    expense_type: 'transport',
    amount: 88
  })

  assert.equal(request.approvalKey, 'in_progress')
  assert.equal(request.node, '直属领导审批')
})

test('normalizes returned backend claims as editable pending submission', () => {
  const request = normalizeRequestForUi({
    id: 'EXP-202605-002',
    claim_id: 'claim-2',
    status: 'returned',
    approval_stage: '待提交',
    expense_type: 'transport',
    amount: 66
  })

  assert.equal(request.approvalKey, 'supplement')
  assert.equal(request.approvalStatus, '待提交')
  assert.equal(request.node, '待提交')
})

test('does not show manager email as direct supervisor name', () => {
  const request = normalizeRequestForUi({
    id: 'EXP-202605-003',
    claim_id: 'claim-3',
    status: 'submitted',
    approval_stage: '直属领导审批',
    expense_type: 'transport',
    amount: 66,
    manager_name: 'manager@example.com'
  })

  assert.equal(request.profileManager, '待补充')
})

test('detects archived claim view models for delete permission gating', () => {
  assert.equal(
    isArchivedRequestView({
      status: 'approved',
      approval_stage: '归档入账',
      approvalKey: 'completed'
    }),
    true
  )
  assert.equal(
    isArchivedRequestView({
      status: 'submitted',
      approval_stage: '财务审批',
      approvalKey: 'in_progress'
    }),
    false
  )
  assert.equal(
    isArchivedRequestView({
      status: 'pending_payment',
      approval_stage: '待付款',
      approvalKey: 'pending_payment'
    }),
    false
  )
  assert.equal(
    isArchivedRequestView({
      status: 'paid',
      approval_stage: '已付款',
      approvalKey: 'completed'
    }),
    true
  )
  assert.equal(
    isArchivedRequestView({
      status: 'approved',
      approval_stage: '审批完成',
      claim_no: 'AP-20260525120000-ABCDEFGH',
      expense_type: 'travel_application',
      approvalKey: 'completed'
    }),
    true
  )
  assert.equal(
    isArchivedRequestView({
      status: 'approved',
      approval_stage: '审批完成',
      claim_no: 'RE-20260525120000-HGFEDCBA',
      expense_type: 'travel',
      approvalKey: 'completed'
    }),
    false
  )
})

test('normalizes pending payment backend claims', () => {
  const request = normalizeRequestForUi({
    id: 'EXP-202605-004',
    claim_id: 'claim-4',
    status: 'pending_payment',
    approval_stage: '待付款',
    expense_type: 'transport',
    amount: 88
  })

  assert.equal(request.approvalKey, 'pending_payment')
  assert.equal(request.approvalStatus, '待付款')
  assert.equal(request.node, '待付款')
})
