const APPLICATION_SESSION_TYPE = 'application'
const APPLICATION_ASSISTANT_ECHO_PATTERN = /(?:这是)?费用申请核对结果|申请核对结果|请核对上述信息|确认无误后.*提交至审批流程/
const APPLICATION_QUERY_PATTERN = /查询|状态|进度|列表|有哪些|材料清单|需要哪些|制度|标准|规则|怎么规定/
const APPLICATION_CREATE_PATTERN = /申请|发起|提交|创建|新建|事前|前置|出差|差旅|采购|会务|会议|培训/
const APPLICATION_REASON_PATTERN = /支撑|支持|部署|上线|实施|驻场|拜访|验收|培训|协助|处理|办理|参加|服务/

export function evaluateLocalApplicationIntentGate(rawText, options = {}) {
  const compact = compactText(rawText)
  if (String(options.sessionType || '').trim() !== APPLICATION_SESSION_TYPE) {
    return block('out_of_scope', 0.98, '不是申请会话。')
  }
  if (options.systemGenerated || options.reviewAction || Number(options.attachmentCount || 0) > 0) {
    return block('out_of_scope', 0.96, '系统生成、审核动作或附件场景不走本地申请预览。')
  }
  if (!compact) {
    return block('chitchat_or_noise', 0.95, '输入为空。')
  }
  if (APPLICATION_ASSISTANT_ECHO_PATTERN.test(compact)) {
    return block('assistant_echo', 0.96, '用户输入是助手申请核对文案回显。')
  }
  if (APPLICATION_QUERY_PATTERN.test(compact)) {
    return block('ask_question', 0.86, '用户更像是在查询或询问。')
  }

  const fields = collectLocalApplicationIntentFields(rawText)
  const fieldCount = Object.keys(fields).length
  const hasCreateSignal = APPLICATION_CREATE_PATTERN.test(compact)
  if (hasCreateSignal && fieldCount >= 2) {
    return allow('create_application', 0.78, '申请动作和结构化申请事实同时存在。', fields)
  }
  if (hasCreateSignal && fieldCount === 1 && hasStrongBusinessSignal(compact)) {
    return allow('create_application', 0.72, '申请动作和明确业务事实同时存在。', fields)
  }
  return block(
    hasCreateSignal ? 'unknown' : 'chitchat_or_noise',
    hasCreateSignal ? 0.58 : 0.82,
    '未识别到足够的新建申请意图。',
    fields
  )
}

function allow(intent, confidence, reason, fields = {}) {
  return {
    intent,
    allowed: true,
    confidence,
    reason,
    fields
  }
}

function block(intent, confidence, reason, fields = {}) {
  return {
    intent,
    allowed: false,
    confidence,
    reason,
    fields
  }
}

function collectLocalApplicationIntentFields(rawText) {
  const text = String(rawText || '')
  const compact = compactText(text)
  const fields = {}
  if (/(?:20\d{2}[-/.年]\d{1,2}[-/.月]\d{1,2}|[1-9]\d?月[1-3]?\d日?)/.test(compact)) {
    fields.time = 'time_text'
  }
  if (/(?:\d+|[一二两三四五六七八九十]{1,3})天/.test(compact)) {
    fields.days = 'days_text'
  }
  if (/\d+(?:\.\d+)?\s*(?:万|千|k|K|元|块|人民币)/.test(text)) {
    fields.amount = 'amount_text'
  }
  if (/(?:飞机|机票|航班|火车|高铁|动车|轮船|船票|客轮|渡轮)/.test(compact)) {
    fields.transportMode = 'transport_text'
  }
  if (/(?:去|到|赴|前往)[\u4e00-\u9fa5]{1,24}(?:出差|支撑|支持|部署|开会|培训|拜访|验收|项目|客户|$)/.test(compact)) {
    fields.location = 'location_text'
  }
  if (APPLICATION_REASON_PATTERN.test(compact)) {
    fields.reason = 'reason_text'
  }
  return fields
}

function hasStrongBusinessSignal(compactTextValue) {
  return APPLICATION_REASON_PATTERN.test(compactTextValue) || /出差|差旅/.test(compactTextValue)
}

function compactText(value) {
  return String(value || '').replace(/\s+/g, '')
}
