export const SKIP_REQUIRED_APPLICATION_LINK_ACTION = 'skip_required_application_link'
export const SKIP_REIMBURSEMENT_DRAFT_CHECK_ACTION = 'skip_reimbursement_draft_check'
export const CONTINUE_REIMBURSEMENT_DRAFT_ACTION = 'continue_reimbursement_draft_association'
export const CREATE_STANDALONE_REIMBURSEMENT_DRAFT_ACTION = 'create_standalone_reimbursement_draft'
export const CANCEL_STANDALONE_REIMBURSEMENT_DRAFT_ACTION = 'cancel_standalone_reimbursement_draft'

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

function resolvePrimaryReimbursementDraft(drafts = []) {
  return (Array.isArray(drafts) ? drafts : []).find((draft) => (
    normalizeText(draft?.id || draft?.claim_id || draft?.claimId || draft?.claim_no || draft?.claimNo)
  )) || {}
}

function resolveDraftClaimNo(draft = {}) {
  return normalizeText(draft.claim_no || draft.claimNo) || '未编号草稿'
}

function resolveDraftClaimId(draft = {}) {
  return normalizeText(draft.id || draft.claim_id || draft.claimId)
}

export function buildSkipRequiredApplicationLinkAction(originalMessage = '') {
  return {
    label: '不关联，单独新建报销单',
    description: '跳过申请单关联，继续选择报销类型并新建报销单。',
    icon: 'mdi mdi-file-plus-outline',
    action_type: SKIP_REQUIRED_APPLICATION_LINK_ACTION,
    payload: {
      original_message: normalizeText(originalMessage) || '我要报销'
    }
  }
}

export function buildSkipReimbursementDraftCheckAction(originalMessage = '') {
  return {
    label: '不用草稿，关联申请单新建报销单',
    description: '跳过已有报销草稿，继续查询可关联申请单。',
    icon: 'mdi mdi-file-search-outline',
    action_type: SKIP_REIMBURSEMENT_DRAFT_CHECK_ACTION,
    payload: {
      original_message: normalizeText(originalMessage) || '我要报销'
    }
  }
}

export function buildViewReimbursementDraftAction(draft = {}, originalMessage = '') {
  const claimNo = resolveDraftClaimNo(draft)
  return {
    label: `查看草稿 ${claimNo}`,
    description: '打开草稿详情页核对已填内容。',
    icon: 'mdi mdi-file-eye-outline',
    action_type: 'open_application_detail',
    payload: {
      claim_id: resolveDraftClaimId(draft),
      claim_no: normalizeText(draft.claim_no || draft.claimNo),
      original_message: normalizeText(originalMessage) || '我要报销'
    }
  }
}

export function buildContinueReimbursementDraftAction(draft = {}, originalMessage = '') {
  const claimNo = resolveDraftClaimNo(draft)
  return {
    label: `继续关联草稿 ${claimNo}`,
    description: '上传相关附件或补充说明，继续完善这张草稿。',
    icon: 'mdi mdi-link-variant',
    action_type: CONTINUE_REIMBURSEMENT_DRAFT_ACTION,
    payload: {
      claim_id: resolveDraftClaimId(draft),
      claim_no: normalizeText(draft.claim_no || draft.claimNo),
      original_message: normalizeText(originalMessage) || '我要报销'
    }
  }
}

export function buildCreateStandaloneReimbursementDraftAction(originalMessage = '') {
  return {
    label: '独立新建报销单',
    description: '不使用当前草稿，先确认是否创建新的报销草稿。',
    icon: 'mdi mdi-file-plus-outline',
    action_type: CREATE_STANDALONE_REIMBURSEMENT_DRAFT_ACTION,
    payload: {
      original_message: normalizeText(originalMessage) || '我要报销'
    }
  }
}

export function buildStandaloneReimbursementDraftConfirmationText() {
  return [
    '是否新建草稿单据？',
    '',
    '确认后我会跳过当前草稿，按新的报销单据继续收集类型、附件和说明。'
  ].join('\n')
}

export function buildStandaloneReimbursementDraftConfirmationActions(originalMessage = '') {
  const sourceText = normalizeText(originalMessage) || '我要报销'
  return [
    {
      label: '新建草稿单据',
      description: '进入单独新建报销单流程。',
      icon: 'mdi mdi-file-plus-outline',
      action_type: SKIP_REQUIRED_APPLICATION_LINK_ACTION,
      payload: {
        original_message: sourceText,
        standalone_draft_confirmed: true
      }
    },
    {
      label: '暂不新建',
      description: '保留当前对话，不创建新的报销草稿。',
      icon: 'mdi mdi-close-circle-outline',
      action_type: CANCEL_STANDALONE_REIMBURSEMENT_DRAFT_ACTION,
      payload: {
        original_message: sourceText
      }
    }
  ]
}

export function buildReimbursementDraftContinuationText(draft = {}) {
  const claimNo = normalizeText(draft.claim_no || draft.claimNo) || '当前草稿'
  return [
    `已选择继续关联草稿 ${claimNo}。`,
    '',
    '请上传相关的附件，或者补充说明。',
    '',
    '收到后我会围绕这张草稿继续整理票据和报销信息。'
  ].join('\n')
}

export function buildReimbursementDraftActions(drafts, originalMessage = '') {
  const sourceText = normalizeText(originalMessage) || '我要报销'
  const primaryDraft = resolvePrimaryReimbursementDraft(drafts)
  return [
    buildViewReimbursementDraftAction(primaryDraft, sourceText),
    buildContinueReimbursementDraftAction(primaryDraft, sourceText),
    buildCreateStandaloneReimbursementDraftAction(sourceText)
  ]
}
