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

import {
  excludeArchivedDocumentRows,
  filterApplicationScopeNewRows,
  prepareApplicationScopeRows,
  isArchivedDocumentRow
} from '../src/utils/documentCenterRows.js'

test('document center archived rows are detected from archive flag or request stage', () => {
  assert.equal(isArchivedDocumentRow({ archived: true }), true)
  assert.equal(
    isArchivedDocumentRow({
      rawRequest: { status: 'approved', approval_stage: '归档入账' }
    }),
    true
  )
  assert.equal(
    isArchivedDocumentRow({
      rawRequest: {
        status: 'approved',
        approval_stage: '审批完成',
        claim_no: 'AP-20260525120000-ABCDEFGH',
        expense_type: 'travel_application'
      }
    }),
    false
  )
  assert.equal(
    isArchivedDocumentRow({
      rawRequest: {
        status: 'approved',
        approval_stage: '申请归档',
        claim_no: 'AP-20260525120000-ABCDEFGH',
        expense_type: 'travel_application'
      }
    }),
    true
  )
  assert.equal(
    isArchivedDocumentRow({
      rawRequest: { status: 'in_progress', approval_stage: '部门审批' }
    }),
    false
  )
  assert.equal(
    isArchivedDocumentRow({
      rawRequest: { status: 'pending_payment', approval_stage: '待付款' }
    }),
    false
  )
  assert.equal(
    isArchivedDocumentRow({
      rawRequest: { status: 'paid', approval_stage: '已付款' }
    }),
    true
  )
  assert.equal(
    isArchivedDocumentRow({
      rawRequest: { status: 'approved', approval_stage: '部门审批', approvalKey: 'completed' }
    }),
    false
  )
})

test('document center all scope excludes archived rows from merged lists', () => {
  const rows = excludeArchivedDocumentRows([
    { claimId: 'a', archived: true },
    { claimId: 'b', rawRequest: { status: 'approved', approval_stage: '归档入账' } },
    { claimId: 'c', rawRequest: { status: 'submitted', approval_stage: '部门审批' } },
    { claimId: 'd', rawRequest: { status: 'pending_payment', approval_stage: '待付款' } }
  ])

  assert.deepEqual(rows.map((row) => row.claimId), ['c', 'd'])
})

test('application scope does not mark submitted approval application rows as new', () => {
  const rows = prepareApplicationScopeRows([
    {
      claimId: 'draft-application',
      documentTypeCode: 'application',
      statusGroup: 'draft',
      isNewDocument: true
    },
    {
      claimId: 'submitted-application',
      documentTypeCode: 'application',
      statusGroup: 'in_progress',
      isNewDocument: true
    },
    {
      claimId: 'reimbursement',
      documentTypeCode: 'reimbursement',
      statusGroup: 'in_progress',
      isNewDocument: true
    }
  ])

  assert.deepEqual(rows.map((row) => row.claimId), ['draft-application', 'submitted-application'])
  assert.equal(rows.find((row) => row.claimId === 'draft-application')?.isNewDocument, true)
  assert.equal(rows.find((row) => row.claimId === 'submitted-application')?.isNewDocument, false)
  assert.deepEqual(
    filterApplicationScopeNewRows(rows).map((row) => row.claimId),
    ['draft-application']
  )
})
