import { ref } from 'vue'
import { Message } from '@arco-design/web-vue'
import { fetchPolicyList } from '@/api/ops/alertPolicy'
import type { PolicyOption } from '../types'

/**
 * 策略相关逻辑
 */
export const usePolicy = () => {
  const policyOptions = ref<PolicyOption[]>([])

  /**
   * 获取策略列表
   */
  const fetchPolicyOptions = async () => {
    try {
      const res = await fetchPolicyList({ page_size: 1000 })
      if (res.code === 0 && res.details) {
        policyOptions.value = (res.details.data || []).map((p: any) => ({
          id: p.id,
          name: p.name,
        }))
      }
    } catch (error) {
      console.error('获取策略列表失败:', error)
    }
  }

  /**
   * 获取策略名称
   */
  const getPolicyName = (policyId: number) => {
    const policy = policyOptions.value.find((p) => p.id === policyId)
    return policy?.name || ''
  }

  return {
    policyOptions,
    fetchPolicyOptions,
    getPolicyName,
  }
}