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

const detailTemplate = readFileSync(
  fileURLToPath(new URL('../src/views/TravelRequestDetailView.vue', import.meta.url)),
  'utf8'
)
const detailEntryScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/TravelRequestDetailView.js', import.meta.url)),
  'utf8'
)
const detailSetupScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/travelRequestDetailSetup.js', import.meta.url)),
  'utf8'
)
const detailApprovalFlowScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/useTravelRequestDetailApprovalFlow.js', import.meta.url)),
  'utf8'
)
const detailRiskSubmitScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/useTravelRequestDetailRiskSubmit.js', import.meta.url)),
  'utf8'
)
const detailScript = [detailSetupScript, detailApprovalFlowScript, detailRiskSubmitScript].join('\n')
const detailStyles = readFileSync(
  fileURLToPath(new URL('../src/assets/styles/views/travel-request-detail-view.css', import.meta.url)),
  'utf8'
)
const approvalDialog = readFileSync(
  fileURLToPath(new URL('../src/components/travel/TravelRequestApprovalDialog.vue', import.meta.url)),
  'utf8'
)
const confirmDialog = readFileSync(
  fileURLToPath(new URL('../src/components/shared/ConfirmDialog.vue', import.meta.url)),
  'utf8'
)
const budgetAnalysisComponent = readFileSync(
  fileURLToPath(new URL('../src/components/travel/TravelRequestBudgetAnalysis.vue', import.meta.url)),
  'utf8'
)
const reimbursementService = readFileSync(
  fileURLToPath(new URL('../src/services/reimbursements.js', import.meta.url)),
  'utf8'
)
const appShellScript = readFileSync(
  fileURLToPath(new URL('../src/composables/useAppShell.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('approval-mode detail collects leader opinion inside confirm dialog before API call', () => {
  assert.match(detailEntryScript, /import \{ useTravelRequestDetailSetup \} from '\.\/travelRequestDetailSetup\.js'/)
  assert.match(detailEntryScript, /setup: useTravelRequestDetailSetup/)
  assert.match(detailSetupScript, /import \{ useTravelRequestDetailApprovalFlow \} from '\.\/useTravelRequestDetailApprovalFlow\.js'/)
  assert.match(detailSetupScript, /useTravelRequestDetailApprovalFlow\(\{/)
  assert.match(detailSetupScript, /approvalRiskConfirmItems: riskSubmit\.approvalRiskConfirmItems/)
  assert.match(detailEntryScript, /approvalMode:/)
  assert.match(detailScript, /const leaderOpinion = ref\(''\)/)
  assert.match(detailScript, /const approveConfirmDialogOpen = ref\(false\)/)
  assert.match(detailScript, /const approvalRiskConfirmed = ref\(false\)/)
  assert.match(detailScript, /const approvalRiskConfirmItems = computed/)
  assert.match(detailScript, /const approvalRiskConfirmRequired = computed/)
  assert.match(detailScript, /const canApproveRequest = computed/)
  assert.match(detailScript, /canApproveLeaderExpenseClaims/)
  assert.match(detailScript, /canApproveBudgetExpenseApplications/)
  assert.match(detailScript, /isCurrentDirectManagerForRequest/)
  assert.match(detailScript, /isCurrentRequestApplicant/)
  assert.match(detailScript, /isFinanceApprovalStage/)
  assert.match(detailScript, /const isBudgetApprovalStage = computed/)
  assert.match(detailScript, /const showBudgetAnalysis = computed/)
  assert.match(detailScript, /const isCurrentApplicant = computed/)
  assert.match(detailScript, /const isCurrentDirectManagerApprover = computed/)
  assert.match(detailScript, /const canProcessFinanceApprovalStage = computed/)
  assert.match(detailScript, /const canProcessBudgetApprovalStage = computed/)
  assert.match(detailScript, /const canProcessCurrentApprovalStage = computed/)
  assert.match(detailScript, /approvalOpinionTitle/)
  assert.match(detailScript, /approvalConfirmDescription/)
  assert.doesNotMatch(detailScript, /approvalNextStage/)
  assert.doesNotMatch(detailScript, /showApplicationLeaderOpinionInput/)
  assert.doesNotMatch(detailScript, /showLeaderApprovalPanel/)
  assert.match(detailScript, /const budgetApprovalOpinionRequired = computed/)
  assert.match(detailScript, /const requiresApprovalOpinion = computed\(\(\) => budgetApprovalOpinionRequired\.value\)/)
  assert.match(detailScript, /hasBudgetApprovalWarning\(request\.value\)/)
  assert.match(detailScript, /return '预算审批意见'/)
  assert.match(detailScript, /buildLeaderApprovalEvents/)
  assert.match(detailScript, /buildLeaderApprovalInfo/)
  assert.match(detailScript, /const leaderApprovalEvents = computed/)
  assert.match(detailScript, /const hasLeaderApprovalEvents = computed/)
  assert.match(detailScript, /const hasSingleLeaderApprovalEvent = computed\(\(\) => leaderApprovalEvents\.value\.length === 1\)/)
  assert.match(
    detailScript,
    /const showApplicationLeaderOpinion = computed\(\(\) => \(\s*isApplicationDocument\.value\s*&& hasLeaderApprovalEvents\.value\s*\)\)/
  )
  assert.match(detailScript, /isDirectManagerApprovalStage\.value\)[\s\S]*return isCurrentDirectManagerApprover\.value/)
  assert.match(detailScript, /if \(isDirectManagerApprovalStage\.value\) \{[\s\S]*return isCurrentDirectManagerApprover\.value/)
  assert.match(detailScript, /canProcessFinanceApprovalStage\.value/)
  assert.match(detailScript, /canProcessBudgetApprovalStage\.value/)
  assert.match(detailScript, /const canApproveRequest = computed\(\(\) =>\s*request\.value\.approvalKey === 'in_progress'[\s\S]*&& canProcessCurrentApprovalStage\.value\s*\)/)
  assert.doesNotMatch(
    detailScript,
    /const canApproveRequest = computed\(\(\) =>\s*\(Boolean\(props\.approvalMode\) \|\| isApplicationDocument\.value\)/
  )
  assert.doesNotMatch(detailScript, /leaderApprovalReadonlyText/)
  assert.match(detailScript, /resolveGeneratedDraftClaimNo/)
  assert.match(detailScript, /resolveApproveErrorMessage/)
  assert.match(detailScript, /当前部门未配置 P8 预算审批人，请联系管理员配置后再审批。/)
  assert.match(detailScript, /预算已超过警戒值，请填写预算审批意见后再通过。/)
  assert.match(detailScript, /approveActionLabel/)
  assert.match(detailScript, /approveExpenseClaim\(request\.value\.claimId, \{[\s\S]*opinion: leaderOpinion\.value\.trim\(\) \|\| '同意'/)
  assert.match(detailScript, /报销草稿 \$\{generatedDraftClaimNo\} 已生成/)
  assert.match(detailScript, /按预算与风险结果决定下一步/)
  assert.match(detailScript, /无风险且预算充足将直接完成申请/)

  assert.doesNotMatch(detailTemplate, /v-if="showLeaderApprovalPanel"/)
  assert.doesNotMatch(detailTemplate, /showApplicationLeaderOpinionInput/)
  assert.doesNotMatch(detailTemplate, /class="leader-approval-card/)
  assert.doesNotMatch(detailTemplate, /class="inline-leader-opinion/)
  assert.match(detailTemplate, /v-if="showApplicationLeaderOpinion"/)
  assert.match(detailTemplate, /class="application-leader-opinion"/)
  assert.match(detailTemplate, /v-if="hasLeaderApprovalEvents"/)
  assert.match(detailTemplate, /class="application-leader-opinion-timeline"/)
  assert.match(detailTemplate, /:class="\{ 'is-single': hasSingleLeaderApprovalEvent \}"/)
  assert.match(detailTemplate, /v-for="event in leaderApprovalEvents"/)
  assert.match(detailTemplate, /class="application-leader-opinion-event"/)
  assert.match(detailTemplate, /event\.type === 'returned'/)
  assert.match(detailTemplate, /class="application-leader-opinion-event-time"/)
  assert.match(detailTemplate, /event\.dateLabel/)
  assert.match(detailTemplate, /event\.timeLabel/)
  assert.match(detailTemplate, /class="application-leader-opinion-event-rail"/)
  assert.match(detailTemplate, /class="application-leader-opinion-event-status"/)
  assert.match(detailTemplate, /:title="event\.title"/)
  assert.match(detailTemplate, /class="application-leader-opinion-record"/)
  assert.match(detailTemplate, /class="application-leader-opinion-record-head"/)
  assert.match(detailTemplate, /class="application-leader-opinion-record-title"/)
  assert.match(detailTemplate, /class="application-leader-opinion-record-meta"/)
  assert.match(detailTemplate, /event\.operator/)
  assert.match(detailTemplate, /event\.role/)
  assert.match(detailTemplate, /event\.opinion/)
  assert.match(detailTemplate, /class="application-leader-opinion-event-foot"/)
  assert.doesNotMatch(detailTemplate, /leaderApprovalReadonlyText/)
  assert.doesNotMatch(detailTemplate, /\u5f85\u76f4\u5c5e\u9886\u5bfc\u586b\u5199\u5ba1\u6279\u610f\u89c1/)
  assert.match(detailTemplate, /领导意见/)
  assert.match(detailTemplate, /<TravelRequestBudgetAnalysis[\s\S]*v-if="showBudgetAnalysis"[\s\S]*:claim-id="request\.claimId"/)
  assert.match(approvalDialog, /\{\{ opinionTitle \}\}/)
  assert.doesNotMatch(detailTemplate, /v-model="leaderOpinion"/)
  assert.match(detailTemplate, /@click="handleApproveRequest"/)
  assert.match(detailTemplate, /\{\{ approveBusy \? approveBusyLabel : approveActionLabel \}\}/)
  assert.match(detailTemplate, /:open="approveConfirmDialogOpen"/)
  assert.match(detailTemplate, /:badge="approvalConfirmBadge"/)
  assert.match(detailTemplate, /:description="approvalConfirmDescription"/)
  assert.match(detailTemplate, /:confirm-text="approveConfirmText"/)
  assert.match(detailTemplate, /:busy-text="approveBusyText"/)
  assert.match(detailTemplate, /:risk-confirm-required="approvalRiskConfirmRequired"/)
  assert.match(detailTemplate, /v-model:risk-confirmed="approvalRiskConfirmed"/)
  assert.match(detailTemplate, /:risk-confirm-items="approvalRiskConfirmItems"/)
  assert.doesNotMatch(detailTemplate, /:next-stage="approvalNextStage"/)
  assert.match(approvalDialog, /size="approval"/)
  assert.match(approvalDialog, /actions-align="end"/)
  assert.doesNotMatch(approvalDialog, /submit-confirm-summary/)
  assert.doesNotMatch(approvalDialog, /单据编号/)
  assert.doesNotMatch(approvalDialog, /当前节点/)
  assert.match(detailTemplate, /v-model:opinion="leaderOpinion"/)
  assert.match(detailTemplate, /:opinion-placeholder="approvalOpinionPlaceholder"/)
  assert.match(detailTemplate, /:opinion-hint="approvalOpinionHint"/)
  assert.match(detailTemplate, /:opinion-required="requiresApprovalOpinion"/)
  assert.match(detailTemplate, /@confirm="confirmApproveRequest"/)
  assert.match(detailTemplate, /:description="returnDialogDescription"/)
  assert.match(detailTemplate, /:application="isApplicationDocument"/)

  const handleApproveRequest = extractFunction(detailScript, 'handleApproveRequest')
  const confirmApproveRequest = extractFunction(detailScript, 'confirmApproveRequest')
  assert.doesNotMatch(handleApproveRequest, /approveExpenseClaim/)
  assert.doesNotMatch(handleApproveRequest, /leaderOpinion\.value\.trim/)
  assert.match(handleApproveRequest, /approvalRiskConfirmed\.value = !approvalRiskConfirmRequired\.value/)
  assert.match(confirmApproveRequest, /approveExpenseClaim/)
  assert.match(confirmApproveRequest, /approvalRiskConfirmRequired\.value && !approvalRiskConfirmed\.value/)
  assert.match(confirmApproveRequest, /请先确认已核对风险说明和佐证材料，再继续审批。/)
  assert.match(confirmApproveRequest, /requiresApprovalOpinion\.value && !leaderOpinion\.value\.trim\(\)/)
  assert.match(confirmApproveRequest, /预算已超过警戒值，请填写预算审批意见后再通过。/)
  assert.match(confirmApproveRequest, /emit\('request-updated', \{[\s\S]*claimId: request\.value\.claimId,[\s\S]*claim: responsePayload[\s\S]*\}\)[\s\S]*emit\('backToRequests'\)/)
  assert.doesNotMatch(confirmApproveRequest, /请先填写领导意见，填写后才能确认审核。/)
  assert.match(appShellScript, /async function handleRequestUpdated\(payload = \{\}\)/)
  assert.match(appShellScript, /const mappedRequest = mapExpenseClaimToRequest\(payload\.claim\)/)
  assert.match(appShellScript, /upsertRequestSnapshot\(mappedRequest\)/)

  assert.match(approvalDialog, /<textarea/)
  assert.match(approvalDialog, /update:opinion/)
  assert.match(approvalDialog, /opinionPlaceholder/)
  assert.match(approvalDialog, /opinionHint/)
  assert.match(approvalDialog, /opinionRequired/)
  assert.match(approvalDialog, /\{\{ currentOpinion\.length \}\}\/500/)
  assert.match(approvalDialog, /风险说明确认/)
  assert.match(approvalDialog, /riskConfirmRequired/)
  assert.match(approvalDialog, /update:risk-confirmed/)
  assert.match(approvalDialog, /:confirm-disabled="confirmDisabled"/)
  assert.match(approvalDialog, /props\.opinionRequired && !currentOpinion\.value\.trim\(\)/)
  assert.match(approvalDialog, /\.approval-opinion-field \{[\s\S]*gap: 6px;[\s\S]*margin-top: 8px;/)
  assert.match(approvalDialog, /\.approval-opinion-field textarea \{[\s\S]*min-height: 74px;/)
  assert.match(confirmDialog, /confirmDisabled:\s*\{\s*type:\s*Boolean,\s*default:\s*false\s*\}/)
  assert.match(confirmDialog, /:disabled="busy \|\| confirmDisabled"/)
  assert.match(confirmDialog, /\.shared-confirm-card--approval \{[\s\S]*width: min\(460px, calc\(100vw - 40px\)\);/)
  assert.match(confirmDialog, /\.shared-confirm-card--approval h4 \{[\s\S]*font-size: 20px;/)
  assert.match(confirmDialog, /\.shared-confirm-card--approval \.shared-confirm-body \{[\s\S]*max-height: min\(270px, calc\(100dvh - 238px\)\);/)
  assert.match(confirmDialog, /\.shared-confirm-card--approval \.shared-confirm-btn \{[\s\S]*min-width: 118px;[\s\S]*min-height: 38px;/)

  const leaderOpinionPanelRule = detailStyles.match(/^\.application-leader-opinion \{[\s\S]*?\n\}/m)?.[0] ?? ''
  const leaderOpinionTimelineRule = detailStyles.match(/^\.application-leader-opinion-timeline \{[\s\S]*?\n\}/m)?.[0] ?? ''
  const leaderOpinionEventRule = detailStyles.match(/^\.application-leader-opinion-event \{[\s\S]*?\n\}/m)?.[0] ?? ''

  assert.match(detailStyles, /\.detail-card-title-with-icon \{[\s\S]*display: inline-flex;[\s\S]*align-items: center;[\s\S]*gap: 8px;/)
  assert.match(detailStyles, /\.detail-card-title-with-icon i \{[\s\S]*font-size: 18px;[\s\S]*line-height: 1;/)
  assert.match(detailStyles, /\.application-leader-opinion-head span \{[\s\S]*display: inline-flex;[\s\S]*align-items: center;[\s\S]*gap: 8px;/)
  assert.match(leaderOpinionPanelRule, /border-top: 1px solid #e5edf5;/)
  assert.match(leaderOpinionPanelRule, /background: #ffffff;/)
  assert.doesNotMatch(leaderOpinionPanelRule, /linear-gradient|box-shadow/)
  assert.doesNotMatch(detailStyles, /\.leader-approval-card/)
  assert.doesNotMatch(detailStyles, /\.inline-leader-opinion/)
  assert.match(leaderOpinionTimelineRule, /gap: 0;/)
  assert.match(leaderOpinionTimelineRule, /padding: 2px 0 0;/)
  assert.match(detailStyles, /\.application-leader-opinion-event \{/)
  assert.match(leaderOpinionEventRule, /grid-template-columns: 104px 28px minmax\(0, 1fr\);/)
  assert.match(leaderOpinionEventRule, /padding: 0 0 16px;/)
  assert.doesNotMatch(leaderOpinionEventRule, /linear-gradient|box-shadow/)
  assert.match(detailStyles, /\.application-leader-opinion-event-time \{[\s\S]*justify-items: end;[\s\S]*font-variant-numeric: tabular-nums;/)
  assert.match(detailStyles, /\.application-leader-opinion-event-rail \{[\s\S]*justify-content: center;/)
  assert.match(detailStyles, /\.application-leader-opinion-event-rail::after \{[\s\S]*width: 2px;[\s\S]*background: #e2e8f0;/)
  assert.match(detailStyles, /\.application-leader-opinion-event-status \{[\s\S]*width: 22px;[\s\S]*height: 22px;[\s\S]*border-radius: 999px;/)
  assert.match(detailStyles, /\.application-leader-opinion-record \{[\s\S]*border-bottom: 1px solid #edf2f7;[\s\S]*background: #ffffff;/)
  assert.match(detailStyles, /\.application-leader-opinion-record-meta \{[\s\S]*display: flex;[\s\S]*gap: 6px 16px;/)
  assert.match(detailStyles, /\.application-leader-opinion-record p \{[\s\S]*background: #f8fafc;[\s\S]*font-size: 14px;/)
  assert.match(detailStyles, /\.application-leader-opinion-event\.danger \{[\s\S]*--leader-opinion-tone: #dc2626;/)
  assert.match(detailStyles, /\.application-leader-opinion-event\.success \{[\s\S]*--leader-opinion-tone: #16a34a;/)

  assert.match(reimbursementService, /export function approveExpenseClaim\(claimId, payload = \{\}\)/)
  assert.match(reimbursementService, /\/approve/)
  assert.match(reimbursementService, /export function fetchExpenseClaimBudgetAnalysis/)
  assert.match(reimbursementService, /\/budget-analysis/)
  assert.match(budgetAnalysisComponent, /预算分析/)
  assert.match(budgetAnalysisComponent, /当前预算额度/)
  assert.match(budgetAnalysisComponent, /此次费用占预算/)
  assert.match(budgetAnalysisComponent, /综合评分/)
  assert.match(budgetAnalysisComponent, /fetchExpenseClaimBudgetAnalysis/)
})
