/*
 * SPEC: PJ2026-010103 HWPOD 服务。
 * 实现引用: draft-2026-07-20-hwpod-temporal-split。
 * 责任: 把独立 HWPOD workflow 接到迁移期 Cloud API authority。
 */

import { validateHwpodOperationInput } from "./contracts.ts";

export async function executeHwpodOperation(raw: unknown) {
  const input = validateHwpodOperationInput(raw);
  const response = await fetch(`${input.runtimeApiUrl}/v1/hwpod-node-ops`, {
    method: "POST",
    headers: { "content-type": "application/json", ...(input.authorization ? { authorization: input.authorization } : {}) },
    body: JSON.stringify(input.plan),
    signal: AbortSignal.timeout(900_000)
  });
  const body = await response.json().catch(() => null);
  if (!body || typeof body !== "object") return { ok: false, status: "failed", operationId: input.operationId, httpStatus: response.status, blocker: { code: "hwpod_runtime_response_invalid" } };
  return { ok: response.ok && (body as any).ok !== false, status: (body as any).status ?? (response.ok ? "completed" : "failed"), operationId: input.operationId, response: body };
}
