import http from './http'
import type { OpsAuditEntry } from '@/types/ops'
import { mockOpsExecute, mockOpsAudits } from '@/mock/server'

// 见 auth.ts 中关于 VITE_USE_MOCK 的说明：运维白名单网关默认走真实 `/api/sl/ops/*`，
// 仅在前端 dev 调试时由 `.env.development` 把 VITE_USE_MOCK 设为 'true' 回到假数据。
const MOCK = import.meta.env.VITE_USE_MOCK === 'true'

export interface OpsExecuteReq {
  opCode: string
  targetId: string
  reason?: string
  idempotencyKey?: string
}

export interface OpsExecuteResp {
  ok: boolean
  auditId: string
  message?: string
}

export async function executeOp(req: OpsExecuteReq): Promise<OpsExecuteResp> {
  if (MOCK) return mockOpsExecute(req)
  const { data } = await http.post<OpsExecuteResp>('/sl/ops/execute', req)
  return data
}

export async function listAudits(): Promise<OpsAuditEntry[]> {
  if (MOCK) return mockOpsAudits()
  const { data } = await http.get<OpsAuditEntry[]>('/sl/ops/audits')
  return data
}
