import {
  DATE_INPUT_FORMAT,
  buildBusinessTimeContextFromReviewValues as buildBusinessTimeContextFromReviewValuesModel,
  buildReviewFormContextFromPayload as buildReviewFormContextFromPayloadModel,
  cloneReviewEditFields,
  createEmptyInlineReviewState,
  formatAmountDisplay,
  formatReviewSceneDisplayValue,
  isTravelReviewPayload as isTravelReviewPayloadModel,
  normalizeReviewRiskLevel,
  buildReviewAttachmentStatus,
  resolveReviewTravelTransportType as resolveReviewTravelTransportTypeModel,
  shouldShowReviewFactCard
} from './travelReimbursementReviewModel.js'

const REVIEW_PANEL_SCOPE_OVERVIEW = 'overview'
const REVIEW_PANEL_SCOPE_DOCUMENTS = 'documents'
const REVIEW_PANEL_SCOPE_RISK = 'risk'
const DEPRECATED_REVIEW_RISK_TITLE_KEYWORDS = ['历史报销画像', '用户画像', '制度注意事项', '制度注意']

const REVIEW_RISK_LEVEL_META = {
  high: {
    label: '高风险',
    icon: 'mdi mdi-alert-octagon-outline',
    suggestion: '建议先处理该项，再提交审批；如果属于合理业务场景，请补充说明或附件。'
  },
  medium: {
    label: '中风险',
    icon: 'mdi mdi-alert-circle-outline',
    suggestion: '提交前建议核对业务真实性、附件完整性和规则口径。'
  },
  info: {
    label: '提示',
    icon: 'mdi mdi-information-outline',
    suggestion: '该项主要用于辅助判断，可结合当前单据情况简单核对。'
  },
  low: {
    label: '低风险',
    icon: 'mdi mdi-information-outline',
    suggestion: '该项主要用于辅助判断，可结合当前单据情况简单核对。'
  }
}

const REVIEW_PENDING_SUMMARY_PATTERN = /(^|\n)\s*(?:当前还有|我这边看到还有|下方还有|这笔报销还有|目前还有|还有|这次识别结果里还有|我还需要(?:你|您)确认|当前信息还差|本次报销还有)\s+[^\n]*(?:信息待补充|风险提醒|细节还需要进一步确认)[^\n]*(?:草稿)[^\n]*。\s*/g

export function normalizeReviewPanelScope(scope) {
  const normalized = String(scope || '').trim()
  return [REVIEW_PANEL_SCOPE_OVERVIEW, REVIEW_PANEL_SCOPE_DOCUMENTS, REVIEW_PANEL_SCOPE_RISK].includes(normalized)
    ? normalized
    : ''
}

export function canExposeReviewPanelScope(scope) {
  return Boolean(normalizeReviewPanelScope(scope))
}

export function buildBusinessTimeContextFromReviewValues(values = {}) {
  return buildBusinessTimeContextFromReviewValuesModel(values)
}

export function buildReviewFormContextFromPayload(reviewPayload, inlineState = null) {
  return buildReviewFormContextFromPayloadModel(reviewPayload, inlineState)
}

export function buildReviewCorrectionMessage(fields) {
  const lines = ['请按以下核对后的报销信息更新当前识别结果：']
  for (const item of cloneReviewEditFields(fields)) {
    if (!item.label || (!item.value && !item.required)) {
      continue
    }
    lines.push(`${item.label}：${String(item.value || '').trim() || '待补充'}`)
  }
  return lines.join('\n')
}

export function isTravelReviewPayload(reviewPayload, inlineState = createEmptyInlineReviewState()) {
  return isTravelReviewPayloadModel(reviewPayload, inlineState)
}

export function resolveReviewTravelTransportType(reviewPayload, fallbackText = '') {
  return resolveReviewTravelTransportTypeModel(reviewPayload, fallbackText)
}

export function resolveReviewRiskBriefs(reviewPayload) {
  if (!Array.isArray(reviewPayload?.risk_briefs)) return []
  return reviewPayload.risk_briefs.filter((item) => {
    const title = String(item?.title || '').trim()
    return !DEPRECATED_REVIEW_RISK_TITLE_KEYWORDS.some((keyword) => title.includes(keyword))
  })
}

export function buildReviewFactCards(reviewPayload, inlineState = createEmptyInlineReviewState()) {
  const pendingAttachmentCount = Math.max(0, Number(inlineState.pending_attachment_count || 0))
  const totalAttachmentCount = Math.max(0, Number(inlineState.attachment_count || 0))
  const existingAttachmentCount = Math.max(0, totalAttachmentCount - pendingAttachmentCount)
  const attachmentStatus =
    pendingAttachmentCount > 0
      ? existingAttachmentCount > 0
        ? `已上传 ${existingAttachmentCount} 份，待新增 ${pendingAttachmentCount} 份`
        : `待保存 ${pendingAttachmentCount} 份`
      : totalAttachmentCount > 0
        ? `已上传 ${totalAttachmentCount} 份`
        : buildReviewAttachmentStatus(reviewPayload)

  if (isTravelReviewPayload(reviewPayload, inlineState)) {
    return [
      {
        key: 'occurred_date',
        label: '发生时间',
        value: String(inlineState.occurred_date || '').trim() || '待补充',
        icon: 'mdi mdi-calendar-month-outline',
        editor: 'date',
        modelKey: 'occurred_date',
        placeholder: `例如 ${DATE_INPUT_FORMAT}`
      },
      {
        key: 'amount',
        label: '金额',
        value: formatAmountDisplay(inlineState.amount) || '待补充',
        icon: 'mdi mdi-cash',
        editor: 'amount',
        modelKey: 'amount',
        placeholder: '例如 200.00'
      },
      {
        key: 'transport_type',
        label: '交通类型',
        value: String(inlineState.transport_type || '').trim() || '待确认',
        icon: 'mdi mdi-train-car',
        editor: 'text',
        modelKey: 'transport_type',
        placeholder: '例如 火车/高铁、飞机'
      },
      {
        key: 'hotel_name',
        label: '酒店名称',
        value: String(inlineState.merchant_name || '').trim() || '待补充',
        icon: 'mdi mdi-bed-outline',
        editor: 'text',
        modelKey: 'merchant_name',
        placeholder: '请输入酒店名称'
      },
      {
        key: 'travel_purpose',
        label: '出差事宜',
        value: String(inlineState.reason_value || '').trim() || '待补充',
        icon: 'mdi mdi-briefcase-edit-outline',
        editor: 'textarea',
        modelKey: 'reason_value',
        placeholder: '请填写本次出差的具体工作内容或业务意图',
        wide: true
      }
    ]
  }

  const cards = [
    {
      key: 'occurred_date',
      label: '发生时间',
      value: String(inlineState.occurred_date || '').trim() || '待补充',
      icon: 'mdi mdi-calendar-month-outline',
      editor: 'date',
      modelKey: 'occurred_date',
      placeholder: `例如 ${DATE_INPUT_FORMAT}`
    },
    {
      key: 'amount',
      label: '金额',
      value: formatAmountDisplay(inlineState.amount) || '待补充',
      icon: 'mdi mdi-cash',
      editor: 'amount',
      modelKey: 'amount',
      placeholder: '例如 200.00'
    },
    {
      key: 'scene',
      label: '场景 / 事由',
      value: formatReviewSceneDisplayValue(inlineState),
      icon: 'mdi mdi-silverware-fork-knife',
      editor: 'select',
      modelKey: 'scene_label',
      placeholder: '请选择场景'
    },
    {
      key: 'attachments',
      label: '票据状态',
      value: attachmentStatus,
      icon: 'mdi mdi-file-document-outline',
      editor: 'upload',
      modelKey: 'attachment_names',
      placeholder: ''
    }
  ]

  if (shouldShowReviewFactCard(reviewPayload, 'customer_name', inlineState.customer_name)) {
    cards.splice(cards.length - 1, 0, {
      key: 'customer_name',
      label: '关联客户',
      value: String(inlineState.customer_name || '').trim() || '待补充',
      icon: 'mdi mdi-domain',
      editor: 'text',
      modelKey: 'customer_name',
      placeholder: '请输入客户名称'
    })
  }

  if (shouldShowReviewFactCard(reviewPayload, 'location', inlineState.location)) {
    cards.splice(cards.length - 1, 0, {
      key: 'location',
      label: '业务地点',
      value: String(inlineState.location || '').trim() || '待补充',
      icon: 'mdi mdi-map-marker-outline',
      editor: 'text',
      modelKey: 'location',
      placeholder: '请输入业务地点'
    })
  }

  if (shouldShowReviewFactCard(reviewPayload, 'merchant_name', inlineState.merchant_name)) {
    cards.splice(cards.length - 1, 0, {
      key: 'merchant_name',
      label: '酒店/商户',
      value: String(inlineState.merchant_name || '').trim() || '待补充',
      icon: 'mdi mdi-storefront-outline',
      editor: 'text',
      modelKey: 'merchant_name',
      placeholder: '请输入酒店或商户名称'
    })
  }

  if (shouldShowReviewFactCard(reviewPayload, 'participants', inlineState.participants)) {
    cards.splice(cards.length - 1, 0, {
      key: 'participants',
      label: '同行人员',
      value: String(inlineState.participants || '').trim() || '待补充',
      icon: 'mdi mdi-account-group-outline',
      editor: 'text',
      modelKey: 'participants',
      placeholder: '例如 客户 2 人，我方 1 人'
    })
  }

  return cards
}

function normalizeReviewRiskTitle(title, fallbackTitle) {
  const normalized = String(title || '').trim()
  const fallback = String(fallbackTitle || '风险提示').trim() || '风险提示'
  if (!normalized) return fallback
  const cleaned = normalized
    .replace(/AI\s*预审\s*(暂未通过|未通过|不通过)?/g, '风险提示')
    .replace(/(高风险|中风险|低风险)/g, '')
    .replace(/^[：:\-—\s]+|[：:\-—\s]+$/g, '')
    .trim()
  return cleaned || fallback
}

export function buildReviewRiskItems(reviewPayload) {
  return resolveReviewRiskBriefs(reviewPayload)
    .map((brief, index) => {
      const title = String(brief?.title || '').trim()
      const content = String(brief?.content || '').trim()
      const detail = String(brief?.detail || '').trim()
      const suggestion = String(brief?.suggestion || '').trim()
      const level = normalizeReviewRiskLevel(brief?.level)
      const meta = REVIEW_RISK_LEVEL_META[level] || REVIEW_RISK_LEVEL_META.low
      const fallbackTitle = content ? `风险提示 ${index + 1}` : '风险提示'
      const normalizedTitle = normalizeReviewRiskTitle(title, fallbackTitle)
      const summary = content || normalizedTitle

      if (!normalizedTitle && !summary) return null

      return {
        key: `${level}-${normalizedTitle}-${index}`,
        title: normalizedTitle,
        summary,
        detail: detail || content || '当前风险项没有返回更长解释，建议结合票据、报销事由和规则要求进行复核。',
        level,
        levelLabel: meta.label,
        icon: meta.icon,
        sourceLabel: meta.label,
        suggestion: suggestion || meta.suggestion
      }
    })
    .filter(Boolean)
}

export function buildReviewRiskConversationText(item, detailTarget = {}) {
  const title = String(item?.title || '风险提示').trim()
  const summary = String(item?.summary || '').trim()
  const detail = String(item?.detail || '').trim()
  const suggestion = String(item?.suggestion || '').trim()
  const isInfo = String(item?.level || '').trim() === 'info'
  const detailHref = String(detailTarget?.href || '').trim()
  const detailLabel = String(detailTarget?.label || '').trim() || '进入该单据详情重新填写'
  const lines = [`${title}`]

  if (summary) {
    lines.push('', `${isInfo ? '提示内容' : '风险点'}：${summary}`)
  }
  if (detail && detail !== summary) {
    lines.push('', `规则依据：${detail}`)
  }
  if (suggestion) {
    lines.push('', `${isInfo ? '处理建议' : '修改建议'}：${suggestion}`)
  }
  if (detailHref) {
    lines.push('', `[${detailLabel}](${detailHref})`)
  }
  return lines.join('\n')
}

export function buildReviewMainMessageText(message) {
  const text = String(message?.text || '')
  if (!message?.reviewPayload) {
    return text
  }
  return text
    .replace(REVIEW_PENDING_SUMMARY_PATTERN, '\n')
    .replace(/\n{3,}/g, '\n\n')
    .trim()
}
