import { computed, ref } from 'vue'

import { generateRiskRuleAsset } from '../../services/agentAssets.js'
import { normalizeText } from './auditViewModel.js'
import {
  createDefaultRiskRuleForm
} from './auditViewRiskRuleModel.js'

export function useAuditRiskRuleCreateFlow({
  activeType,
  isRuleManager,
  detailBusy,
  actionState,
  assetBuckets,
  refreshCurrentAssets,
  resolveActor,
  toast
}) {
  const riskRuleCreateOpen = ref(false)
  const riskRuleCreateForm = ref(createDefaultRiskRuleForm())
  const riskRuleGenerationPollTimers = new Map()

  const riskRuleCreateBusy = computed(() => actionState.value === 'generate-risk-rule')
  const canCreateRiskRule = computed(
    () => activeType.value === 'riskRules' && isRuleManager.value && !detailBusy.value
  )

  function openRiskRuleCreateDialog() {
    if (activeType.value !== 'riskRules') {
      return
    }
    riskRuleCreateForm.value = createDefaultRiskRuleForm()
    riskRuleCreateOpen.value = true
  }

  function closeRiskRuleCreateDialog() {
    if (riskRuleCreateBusy.value) {
      return
    }
    riskRuleCreateOpen.value = false
  }

  async function submitRiskRuleCreate() {
    if (!canCreateRiskRule.value || riskRuleCreateBusy.value) {
      return
    }

    const naturalLanguage = String(riskRuleCreateForm.value.natural_language || '').trim()
    const ruleTitle = String(riskRuleCreateForm.value.rule_title || '').trim()
    if (ruleTitle.length < 2) {
      toast('请输入至少 2 个字的规则标题。')
      return
    }
    if (naturalLanguage.length < 8) {
      toast('请至少输入 8 个字的风险规则描述。')
      return
    }

    actionState.value = 'generate-risk-rule'
    try {
      const detail = await generateRiskRuleAsset(
        {
          business_domain: riskRuleCreateForm.value.business_domain || 'expense',
          business_stage: riskRuleCreateForm.value.business_stage,
          expense_category: riskRuleCreateForm.value.expense_category,
          rule_title: ruleTitle,
          requires_attachment: Boolean(riskRuleCreateForm.value.requires_attachment),
          natural_language: naturalLanguage
        },
        { actor: resolveActor() }
      )
      riskRuleCreateOpen.value = false
      await refreshCurrentAssets()
      scheduleRiskRuleGenerationPoll(detail.id)
      toast('风险规则已进入后台生成，列表会先显示生成中。')
    } catch (error) {
      toast(error?.message || '风险规则生成失败，请稍后重试。')
    } finally {
      actionState.value = ''
    }
  }

  function stopRiskRuleGenerationPoll(assetId) {
    const timer = riskRuleGenerationPollTimers.get(assetId)
    if (timer) {
      window.clearTimeout(timer)
      riskRuleGenerationPollTimers.delete(assetId)
    }
  }

  function scheduleRiskRuleGenerationPoll(assetId, attempt = 0) {
    const normalizedAssetId = normalizeText(assetId)
    if (!normalizedAssetId) {
      return
    }

    stopRiskRuleGenerationPoll(normalizedAssetId)
    const timer = window.setTimeout(async () => {
      try {
        await refreshCurrentAssets()
        const latest = (assetBuckets.value.riskRules || []).find((item) => item.id === normalizedAssetId)
        if (!latest || latest.statusValue !== 'generating' || attempt >= 59) {
          riskRuleGenerationPollTimers.delete(normalizedAssetId)
          return
        }
        scheduleRiskRuleGenerationPoll(normalizedAssetId, attempt + 1)
      } catch {
        if (attempt < 59) {
          scheduleRiskRuleGenerationPoll(normalizedAssetId, attempt + 1)
        } else {
          riskRuleGenerationPollTimers.delete(normalizedAssetId)
        }
      }
    }, attempt === 0 ? 1200 : 3000)
    riskRuleGenerationPollTimers.set(normalizedAssetId, timer)
  }

  function stopAllRiskRuleGenerationPolls() {
    riskRuleGenerationPollTimers.forEach((timer) => window.clearTimeout(timer))
    riskRuleGenerationPollTimers.clear()
  }

  return {
    canCreateRiskRule,
    riskRuleCreateOpen,
    riskRuleCreateForm,
    riskRuleCreateBusy,
    openRiskRuleCreateDialog,
    closeRiskRuleCreateDialog,
    submitRiskRuleCreate,
    stopAllRiskRuleGenerationPolls
  }
}
