/**
 * 服务监测 - 定时任务 API
 */
import http from '@/utils/http'
import type {
  Schedule,
  CreateScheduleRequest,
  UpdateScheduleRequest,
} from '@/types/service-monitor'

/** 获取定时任务列表 */
export async function getSchedules(): Promise<{ success: boolean; schedules: Schedule[] }> {
  const res = await http.get<{ success: boolean; schedules: Schedule[] }>(
    '/api/service-monitor/schedules'
  )
  return res.data
}

/** 创建定时任务 */
export async function createSchedule(
  data: CreateScheduleRequest
): Promise<{ success: boolean; schedule: Schedule }> {
  const res = await http.post<{ success: boolean; schedule: Schedule }>(
    '/api/service-monitor/schedules',
    data
  )
  return res.data
}

/** 更新定时任务 */
export async function updateSchedule(
  scheduleId: string,
  data: UpdateScheduleRequest
): Promise<{ success: boolean; schedule: Schedule }> {
  const res = await http.put<{ success: boolean; schedule: Schedule }>(
    `/api/service-monitor/schedules/${scheduleId}`,
    data
  )
  return res.data
}

/** 删除定时任务 */
export async function deleteSchedule(scheduleId: string): Promise<{ success: boolean }> {
  const res = await http.delete<{ success: boolean }>(
    `/api/service-monitor/schedules/${scheduleId}`
  )
  return res.data
}

/** 切换定时任务启用/禁用 */
export async function toggleSchedule(
  scheduleId: string
): Promise<{ success: boolean; schedule: Schedule }> {
  const res = await http.post<{ success: boolean; schedule: Schedule }>(
    `/api/service-monitor/schedules/${scheduleId}/toggle`
  )
  return res.data
}

/** 手动执行定时任务 */
export async function runSchedule(
  scheduleId: string
): Promise<{ success: boolean; schedule_id: string; triggered: boolean }> {
  const res = await http.post<{ success: boolean; schedule_id: string; triggered: boolean }>(
    `/api/service-monitor/schedules/${scheduleId}/run`
  )
  return res.data
}