import http from './http'
import type { AlarmFeed } from '@/types/alarm'

const MOCK = import.meta.env.VITE_USE_MOCK === 'true'

/** 报警管理数据源：平台记录库 /fleet/alarms（离线可读 + 历史保留）。 */
export async function fetchAlarmFeed(limit = 2000): Promise<AlarmFeed> {
  if (MOCK) {
    const { mockAlarms } = await import('@/mock/data/alarms')
    return { online: true, lastSyncAt: new Date().toISOString(), alarms: await mockAlarms() }
  }
  const { data } = await http.get<AlarmFeed>('/fleet/alarms', { params: { limit } })
  return {
    online: !!data?.online,
    lastSyncAt: data?.lastSyncAt ?? null,
    alarms: Array.isArray(data?.alarms) ? data.alarms : []
  }
}
