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 detailViewComponentScript = 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'
)
const detailViewSetupScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/travelRequestDetailSetup.js', import.meta.url)),
  'utf8'
)
const detailSmartEntryScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/travelRequestDetailSmartEntryRecognition.js', import.meta.url)),
  'utf8'
)
const detailAttachmentPreviewScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/useTravelRequestDetailAttachmentPreview.js', import.meta.url)),
  'utf8'
)
const detailExpenseEditorScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/useTravelRequestDetailExpenseEditor.js', import.meta.url)),
  'utf8'
)
const detailRiskSubmitScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/useTravelRequestDetailRiskSubmit.js', import.meta.url)),
  'utf8'
)
const detailApprovalFlowScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/useTravelRequestDetailApprovalFlow.js', import.meta.url)),
  'utf8'
)
const detailViewScript = [
  detailViewComponentScript,
  detailViewSetupScript,
  detailSmartEntryScript,
  detailAttachmentPreviewScript,
  detailExpenseEditorScript,
  detailRiskSubmitScript,
  detailApprovalFlowScript,
  detailExpenseModelScript
].join('\n')
const confirmDialogComponent = readFileSync(
  fileURLToPath(new URL('../src/components/shared/ConfirmDialog.vue', import.meta.url)),
  'utf8'
)
const deleteDialogComponent = readFileSync(
  fileURLToPath(new URL('../src/components/travel/TravelRequestDeleteDialog.vue', import.meta.url)),
  'utf8'
)

function extractFunction(source, name) {
  let signatureIndex = source.indexOf(`async function ${name}(`)
  if (signatureIndex === -1) {
    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`)
}

function createSubmitRunner({
  claimId = 'claim-001',
  preReviewExpenseClaim,
  submitExpenseClaim,
  applyClaimRiskFlagsPayload = () => {},
  toast = () => {},
  emit = () => {}
}) {
  const runSubmitRequest = extractFunction(detailViewScript, 'runSubmitRequest')
  const factory = new Function(
    'preReviewExpenseClaim',
    'submitExpenseClaim',
    'applyClaimRiskFlagsPayload',
    'toast',
    'emit',
    'request',
    'submitConfirmDialogOpen',
    'submitBusy',
    'submitTaskSeq',
    `${runSubmitRequest}; return runSubmitRequest`
  )
  const submitConfirmDialogOpen = { value: true }
  const submitBusy = { value: true }
  return {
    run: factory(
      preReviewExpenseClaim,
      submitExpenseClaim,
      applyClaimRiskFlagsPayload,
      toast,
      emit,
      { value: { claimId } },
      submitConfirmDialogOpen,
      submitBusy,
      1
    ),
    submitConfirmDialogOpen,
    submitBusy
  }
}

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

  assert.match(detailViewScript, /const submitConfirmDialogOpen = ref\(false\)/)
  assert.match(detailViewTemplate, /v-if="submitBusy" class="expense-recognition-banner submit-progress-banner"/)
  assert.match(detailViewScript, /preReviewExpenseClaim/)
  assert.match(detailViewScript, /const submitActionLabel = computed/)
  assert.match(detailViewScript, /const submitConfirmSecondaryText = computed/)
  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.doesNotMatch(handleSubmit, /runAiPreReview\(\)/)
  assert.match(confirmSubmitRequest, /submitConfirmDialogOpen\.value = false[\s\S]*void runSubmitRequest\(claimId, documentNo, isApplication, taskSeq\)/)
  assert.doesNotMatch(confirmSubmitRequest, /await /)
  assert.doesNotMatch(confirmSubmitRequest, /submitExpenseClaim/)
  const runSubmitRequest = extractFunction(detailViewScript, 'runSubmitRequest')
  const preReviewIndex = runSubmitRequest.indexOf('preReviewExpenseClaim(claimId)')
  const submitIndex = runSubmitRequest.indexOf('submitExpenseClaim(claimId,')
  assert.ok(preReviewIndex > -1)
  assert.ok(submitIndex > preReviewIndex)
  assert.match(runSubmitRequest, /pre_review_id:[\s\S]*preReview\?\.review_id/)
  assert.match(runSubmitRequest, /input_fingerprint:[\s\S]*preReview\?\.input_fingerprint/)
  assert.match(runSubmitRequest, /taskSeq !== submitTaskSeq/)
})

test('detail submit uses the ready pre-review identity and fingerprint', async () => {
  const calls = []
  const appliedPayloads = []
  const { run, submitBusy } = createSubmitRunner({
    preReviewExpenseClaim: async (claimId) => {
      calls.push(['pre-review', claimId])
      return {
        claim_risk_flags: [],
        pre_review: {
          decision: 'ready',
          review_id: 'review-ready-001',
          input_fingerprint: 'sha256:ready-input'
        }
      }
    },
    submitExpenseClaim: async (claimId, payload) => {
      calls.push(['submit', claimId, payload])
      return { status: 'submitted', approval_stage: 'manager' }
    },
    applyClaimRiskFlagsPayload: (payload) => appliedPayloads.push(payload)
  })

  await run('claim-001', 'BX-001', false, 1)

  assert.deepEqual(calls, [
    ['pre-review', 'claim-001'],
    [
      'submit',
      'claim-001',
      {
        pre_review_id: 'review-ready-001',
        input_fingerprint: 'sha256:ready-input'
      }
    ]
  ])
  assert.equal(appliedPayloads.length, 1)
  assert.equal(submitBusy.value, false)
})

test('detail submit stops after needs_fix and refreshes visible risks', async () => {
  let submitCalls = 0
  const appliedPayloads = []
  const emitted = []
  const toasts = []
  const reviewedClaim = {
    claim_risk_flags: [{ source: 'policy', severity: 'high' }],
    pre_review: {
      decision: 'needs_fix',
      message: '住宿费超标，请先按标准调整。'
    }
  }
  const { run, submitBusy } = createSubmitRunner({
    preReviewExpenseClaim: async () => reviewedClaim,
    submitExpenseClaim: async () => {
      submitCalls += 1
      return { status: 'submitted' }
    },
    applyClaimRiskFlagsPayload: (payload) => appliedPayloads.push(payload),
    toast: (message) => toasts.push(message),
    emit: (...args) => emitted.push(args)
  })

  await run('claim-001', 'BX-001', false, 1)

  assert.equal(submitCalls, 0)
  assert.deepEqual(appliedPayloads, [reviewedClaim])
  assert.deepEqual(emitted, [['request-updated', { claimId: 'claim-001' }]])
  assert.deepEqual(toasts, ['住宿费超标，请先按标准调整。'])
  assert.equal(submitBusy.value, false)
})

test('structured pre-review 409 requests detail refresh so backend risks are refilled', async () => {
  const emitted = []
  const toasts = []
  const structuredError = Object.assign(new Error('提交前预审已失效。'), {
    code: 'PRE_REVIEW_NEEDS_FIX',
    review: {
      decision: 'needs_fix',
      message: '发票抬头不匹配，请重新上传。',
      findings: [{ risk_id: 'risk-001', severity: 'high' }]
    }
  })
  const { run, submitBusy } = createSubmitRunner({
    preReviewExpenseClaim: async () => ({
      claim_risk_flags: [],
      pre_review: {
        decision: 'ready',
        review_id: 'review-stale',
        input_fingerprint: 'sha256:stale'
      }
    }),
    submitExpenseClaim: async () => {
      throw structuredError
    },
    toast: (message) => toasts.push(message),
    emit: (...args) => emitted.push(args)
  })

  await run('claim-001', 'BX-001', false, 1)

  assert.deepEqual(toasts, ['发票抬头不匹配，请重新上传。'])
  assert.deepEqual(emitted, [['request-updated', { claimId: 'claim-001' }]])
  assert.equal(submitBusy.value, false)
})

test('detail submit warns on missing risk explanation and supports standard adjustment', () => {
  assert.match(detailViewTemplate, /:open="riskOverrideDialogOpen"/)
  assert.match(detailViewTemplate, /:open="riskOverrideDialogOpen"[\s\S]*size="review"/)
  assert.match(detailViewTemplate, /异常说明/)
  assert.match(detailViewTemplate, /:title="riskOverrideDialogTitle"/)
  assert.match(detailViewTemplate, /:description="riskOverrideDialogDescription"/)
  assert.match(detailViewTemplate, /:confirm-text="riskOverrideConfirmText"/)
  assert.match(detailViewTemplate, /@confirm="confirmRiskOverrideDialog"/)
  assert.match(detailViewTemplate, /class="risk-override-card-shell"/)
  assert.match(detailViewTemplate, /class="risk-override-side-nav risk-override-side-nav--previous"/)
  assert.match(detailViewTemplate, /class="risk-override-side-nav risk-override-side-nav--next"/)
  assert.match(detailViewTemplate, /class="risk-override-guidance"/)
  assert.match(detailViewTemplate, /goToPreviousSubmitRisk/)
  assert.match(detailViewTemplate, /goToNextSubmitRisk/)
  assert.doesNotMatch(detailViewTemplate, /class="risk-override-nav"/)
  assert.doesNotMatch(detailViewTemplate, /v-model="riskOverrideReasons\[currentSubmitRiskWarning\.id\]"/)
  assert.doesNotMatch(detailViewTemplate, /risk-override-save-btn/)
  assert.doesNotMatch(detailViewTemplate, /confirmRiskOverrideReasons/)
  assert.match(detailViewScript, /const submitRiskWarnings = computed/)
  assert.match(detailViewScript, /const submitExplainedRiskWarnings = computed/)
  assert.match(detailViewScript, /const submitRiskReviewWarnings = computed/)
  assert.match(detailViewScript, /const hasMissingSubmitRiskWarnings = computed/)
  assert.match(detailViewScript, /const riskOverrideConfirmText = computed\(\(\) =>[\s\S]*确认说明/)
  const handleSubmit = extractFunction(detailViewScript, 'handleSubmit')
  const confirmSubmitRequest = extractFunction(detailViewScript, 'confirmSubmitRequest')
  assert.match(handleSubmit, /submitRiskReviewWarnings\.value\.length[\s\S]*openRiskOverrideDialog\(\)/)
  assert.doesNotMatch(confirmSubmitRequest, /openRiskOverrideDialog/)
  assert.doesNotMatch(detailViewScript, /riskOverrideReasons/)
  assert.doesNotMatch(detailViewScript, /function confirmRiskOverrideReasons\(\)/)
  assert.match(detailViewScript, /function confirmRiskOverrideDialog\(\)/)
  assert.match(detailViewScript, /function confirmRiskExplanation\(\)/)
  assert.match(detailViewScript, /function confirmStandardAdjustment\(\)/)
  assert.match(detailViewTemplate, /v-if="standardAdjustmentBusy" class="expense-recognition-banner standard-adjustment-banner"/)
  assert.match(detailViewScript, /const standardAdjustmentBusy = ref\(false\)/)
  const confirmRiskExplanation = extractFunction(detailViewScript, 'confirmRiskExplanation')
  assert.match(confirmRiskExplanation, /riskOverrideDialogOpen\.value = false[\s\S]*submitConfirmDialogOpen\.value = true/)
  const confirmStandardAdjustment = extractFunction(detailViewScript, 'confirmStandardAdjustment')
  assert.match(confirmStandardAdjustment, /const claimId = String\(request\.value\?\.claimId/)
  assert.match(confirmStandardAdjustment, /riskOverrideDialogOpen\.value = false[\s\S]*standardAdjustmentBusy\.value = true[\s\S]*void runStandardAdjustmentRecalculation\(claimId, taskSeq\)/)
  assert.doesNotMatch(confirmStandardAdjustment, /await /)
  assert.doesNotMatch(confirmStandardAdjustment, /acceptExpenseClaimStandardAdjustment/)
  const runStandardAdjustmentRecalculation = extractFunction(detailViewScript, 'runStandardAdjustmentRecalculation')
  assert.match(runStandardAdjustmentRecalculation, /acceptExpenseClaimStandardAdjustment\(claimId, payload\)/)
  assert.doesNotMatch(runStandardAdjustmentRecalculation, /submitConfirmDialogOpen\.value = true/)
  assert.match(detailViewScript, /buildStandardAdjustmentPayloadModel\(\{[\s\S]*warnings:\s*submitRiskCards\.value/)
  const actionBusyStart = detailViewScript.indexOf('actionBusy = computed')
  const actionBusyEnd = detailViewScript.indexOf('const profile = computed', actionBusyStart)
  assert.ok(actionBusyStart > -1 && actionBusyEnd > actionBusyStart)
  assert.doesNotMatch(detailViewScript.slice(actionBusyStart, actionBusyEnd), /standardAdjustmentBusy/)
  assert.match(detailExpenseModelScript, /STANDARD_ADJUSTMENT_RISK_SOURCE = 'reimbursement_standard_adjustment'/)
})

test('submit confirm dialog is constrained for laptop viewport height', () => {
  assert.match(confirmDialogComponent, /max-height:\s*calc\(100vh - 40px\)/)
  assert.match(confirmDialogComponent, /max-height:\s*calc\(100dvh - 40px\)/)
  assert.match(confirmDialogComponent, /\.shared-confirm-body \{[\s\S]*min-height: 0;[\s\S]*overflow-y: auto;/)
  assert.match(confirmDialogComponent, /\.shared-confirm-actions \{[\s\S]*flex: 0 0 auto;/)
  assert.match(confirmDialogComponent, /\.shared-confirm-card--review \{[\s\S]*width: min\(560px, calc\(100vw - 40px\)\);/)
  assert.match(confirmDialogComponent, /@media \(max-width: 720px\) \{[\s\S]*max-height: calc\(100dvh - 28px\)/)
})

test('delete request dialog uses a compact destructive confirmation layout', () => {
  assert.match(deleteDialogComponent, /size="destructive"/)
  assert.match(deleteDialogComponent, /actions-align="end"/)
  assert.match(detailViewScript, /const deleteDialogTarget = computed\(\(\) => request\.value\.documentNo \|\| request\.value\.id \|\| '当前单据'\)/)
  assert.match(detailViewScript, /const deleteDialogTitle = computed\(\(\) => `确认\$\{deleteActionLabel\.value\}吗？`\)/)
  assert.doesNotMatch(detailViewScript, /const deleteDialogTitle = computed\(\(\) => `确认\$\{deleteActionLabel\.value\} \$\{request\.value\.id\} 吗？`\)/)
  assert.match(confirmDialogComponent, /\.shared-confirm-card--destructive \{[\s\S]*width: min\(420px, calc\(100vw - 40px\)\);/)
  assert.match(confirmDialogComponent, /\.shared-confirm-card--destructive h4 \{[\s\S]*font-size: 19px;/)
  assert.match(confirmDialogComponent, /\.shared-confirm-card--destructive \.shared-confirm-btn \{[\s\S]*min-width: 112px;[\s\S]*min-height: 38px;/)
})

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

test('detail delete action allows admins or the applicant while the request is editable', () => {
  assert.match(detailViewScript, /const canDeleteRequest = computed\(\(\) => \{/)
  assert.match(detailViewScript, /if \(isPlatformAdminUser\(currentUser\.value\)\) \{[\s\S]*return true/)
  assert.match(detailViewScript, /return isApplicantDeletableRequest\.value/)
  assert.match(detailViewScript, /const isApplicantDeletableRequest = computed\(\(\) => \{/)
  assert.match(detailViewScript, /isCurrentApplicant\.value/)
  assert.match(detailViewScript, /\['draft', 'supplement', 'returned'\]\.includes\(status\)/)
  assert.match(detailViewTemplate, /v-else-if="canReturnRequest \|\| canApproveRequest \|\| canPayRequest \|\| canDeleteRequest"/)
  assert.doesNotMatch(detailViewTemplate, /v-if="canManageCurrentClaim"/)
})

test('detail delete action does not allow in-progress applicant or claim manager fallback', () => {
  const canDeleteStart = detailViewScript.indexOf('const canDeleteRequest = computed')
  const canDeleteEnd = detailViewScript.indexOf('\n  const isDirectManagerApprovalStage', canDeleteStart)
  assert.ok(canDeleteStart >= 0)
  assert.ok(canDeleteEnd > canDeleteStart)
  const canDeleteBlock = detailViewScript.slice(canDeleteStart, canDeleteEnd)
  assert.doesNotMatch(canDeleteBlock, /canManageCurrentClaim/)
  assert.match(detailViewScript, /if \(isApplicationDocument\.value\) {\s*return '删除申请'\s*}/)
  assert.match(detailViewScript, /当前申请单已进入审批流程，只有草稿、待补充或退回待提交阶段的申请人本人或系统管理员可以删除。/)
})
