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

const detailViewTemplate = readFileSync(
  fileURLToPath(new URL('../src/views/TravelRequestDetailView.vue', import.meta.url)),
  'utf8'
)
const detailViewScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/TravelRequestDetailView.js', import.meta.url)),
  'utf8'
)
const detailExpenseModelScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/travelRequestDetailExpenseModel.js', import.meta.url)),
  'utf8'
)

function extractFunction(source, name) {
  const signatureIndex = source.indexOf(`function ${name}(`)
  assert.notEqual(signatureIndex, -1, `${name} should exist`)

  const bodyStart = source.indexOf('{', signatureIndex)
  assert.notEqual(bodyStart, -1, `${name} should have a body`)

  let depth = 0
  for (let index = bodyStart; index < source.length; index += 1) {
    const char = source[index]
    if (char === '{') {
      depth += 1
    } else if (char === '}') {
      depth -= 1
      if (depth === 0) {
        return source.slice(signatureIndex, index + 1)
      }
    }
  }

  assert.fail(`${name} body should be closed`)
}

test('detail submit opens a confirmation dialog before calling submit API', () => {
  assert.match(detailViewTemplate, /<ConfirmDialog[\s\S]*:open="submitConfirmDialogOpen"[\s\S]*confirm-text="确认提交"[\s\S]*@close="closeSubmitConfirmDialog"[\s\S]*@confirm="confirmSubmitRequest"/)
  assert.match(detailViewTemplate, /cancel-text="返回核对"/)
  assert.match(detailViewTemplate, /@click="handleSubmit"/)

  assert.match(detailViewScript, /const submitConfirmDialogOpen = ref\(false\)/)
  assert.match(detailViewScript, /submitConfirmDialogOpen\.value = true/)
  assert.match(detailViewScript, /submitConfirmDialogOpen\.value = false/)
  assert.match(detailViewScript, /submitConfirmDialogOpen,/)
  assert.match(detailViewScript, /closeSubmitConfirmDialog,/)
  assert.match(detailViewScript, /confirmSubmitRequest,/)

  const handleSubmit = extractFunction(detailViewScript, 'handleSubmit')
  const confirmSubmitRequest = extractFunction(detailViewScript, 'confirmSubmitRequest')
  assert.doesNotMatch(handleSubmit, /submitExpenseClaim/)
  assert.match(confirmSubmitRequest, /submitExpenseClaim\(request\.value\.claimId\)/)
})

test('detail submit requires override reasons for high-risk claims', () => {
  assert.match(detailViewTemplate, /:open="riskOverrideDialogOpen"/)
  assert.match(detailViewTemplate, /重大风险/)
  assert.match(detailViewTemplate, /goToPreviousSubmitRisk/)
  assert.match(detailViewTemplate, /goToNextSubmitRisk/)
  assert.match(detailViewTemplate, /v-model="riskOverrideReasons\[currentSubmitRiskWarning\.id\]"/)
  assert.match(detailViewScript, /const submitRiskWarnings = computed/)
  assert.match(detailViewScript, /submitRiskWarnings\.value\.length && !hasRiskOverrideExplanation\.value/)
  assert.match(detailViewScript, /function confirmRiskOverrideReasons\(\)/)
  assert.match(detailViewScript, /updateExpenseClaim\(request\.value\.claimId,\s*\{\s*reason: nextNote/s)
  assert.match(detailViewScript, /超标说明：\$\{tags\}/)
})

test('detail header and fallback progress use reimbursement wording', () => {
  assert.match(detailViewScript, /label:\s*'单据申请日期'/)
  assert.match(detailExpenseModelScript, /label:\s*'创建单据'/)
  assert.doesNotMatch(detailViewScript, /label:\s*'保存草稿'/)
})

test('archived detail delete action is gated by admin-only permission', () => {
  assert.match(detailViewScript, /canDeleteArchivedExpenseClaims/)
  assert.match(detailViewScript, /isArchivedRequestView/)
  assert.match(detailViewScript, /if \(isArchivedRequest\.value\) {\s*return canDeleteArchivedExpenseClaims\(currentUser\.value\)/)
  assert.match(detailViewTemplate, /v-else-if="canReturnRequest \|\| canApproveRequest \|\| canDeleteRequest"/)
  assert.doesNotMatch(detailViewTemplate, /v-if="canManageCurrentClaim"/)
})
