import { isApplicationDocumentNo } from '../../utils/documentClassification.js'

function normalizeText(value) {
  return String(value || '').trim()
}

function cardLikeText(card = {}) {
  if (!card || typeof card !== 'object') {
    return normalizeText(card)
  }
  return [
    card.title,
    card.label,
    card.name,
    card.summary,
    card.message,
    card.reason,
    card.suggestion,
    card.ruleName,
    card.rule_name,
    card.ruleCode,
    card.rule_code,
    card.evidence?.summary,
    card.evidence?.reason
  ].map((value) => normalizeText(value)).filter(Boolean).join(' ')
}

export function normalizeBusinessStage(value) {
  const stage = normalizeText(value).toLowerCase()
  if ([
    'expense_application',
    'application',
    'apply',
    'pre_apply',
    'pre_application',
    'budget_application'
  ].includes(stage)) {
    return 'expense_application'
  }
  if ([
    'reimbursement',
    'expense_reimbursement',
    'claim',
    'expense_claim',
    'expense_report'
  ].includes(stage)) {
    return 'reimbursement'
  }
  return ''
}

export function resolveFlagBusinessStage(flag, fallback = 'reimbursement') {
  if (!flag || typeof flag !== 'object') {
    return resolveRiskTextBusinessStage(flag, fallback)
  }

  const explicitStage = normalizeBusinessStage(
    flag.businessStage
    || flag.business_stage
    || flag.controlStage
    || flag.control_stage
  )
  if (explicitStage) {
    return explicitStage
  }

  const source = normalizeText(flag.source).toLowerCase()
  const eventType = normalizeText(flag.event_type || flag.eventType).toLowerCase()
  if (source === 'attachment_analysis' || /expense_claim|reimbursement|payment/.test(eventType)) {
    return 'reimbursement'
  }
  if (/application/.test(source) || /expense_application/.test(eventType)) {
    return 'expense_application'
  }

  return resolveRiskTextBusinessStage(cardLikeText(flag), fallback)
}

export function resolveRiskTextBusinessStage(value, fallback = 'reimbursement') {
  const text = normalizeText(value)
  if (/报销|附件|单据|票据|发票|OCR|识别|付款|支付|酒店|住宿票|交通票/.test(text)) {
    return 'reimbursement'
  }
  if (/申请|预算|额度|事前|预估|申请金额|申请事由/.test(text)) {
    return 'expense_application'
  }
  return fallback
}

export function resolveRequestBusinessStage(request = {}) {
  const explicitStage = normalizeBusinessStage(
    request?.businessStage
    || request?.business_stage
    || request?.controlStage
    || request?.control_stage
  )
  if (explicitStage) {
    return explicitStage
  }
  const documentType = normalizeText(
    request?.documentTypeCode
    || request?.document_type_code
    || request?.documentType
    || request?.document_type
  ).toLowerCase()
  if (['application', 'expense_application'].includes(documentType)) {
    return 'expense_application'
  }

  const claimNo = normalizeText(
    request?.claimNo
    || request?.claim_no
    || request?.documentNo
    || request?.document_no
    || request?.id
  ).toUpperCase()
  if (isApplicationDocumentNo(claimNo)) {
    return 'expense_application'
  }

  const typeCode = normalizeText(request?.typeCode || request?.expense_type).toLowerCase()
  if (typeCode === 'application' || typeCode.endsWith('_application')) {
    return 'expense_application'
  }
  return 'reimbursement'
}
