type V3ReadKind = 'solvency' | 'status';

/** Static, non-RPC readiness response: it is useful discovery, not live state. */
export function createUnconfiguredV3ReadResponse(kind: V3ReadKind, now = new Date()): Response {
  const body = {
    schema: `vaultfire.v3.protocol.${kind}.v1`,
    schemaVersion: '1.0.0',
    protocolVersion: '3.0.0',
    route: `/api/v3/protocol/${kind}`,
    status: 'not-ready',
    readiness: 'data-source-unconfigured',
    observedAt: now.toISOString(),
    finality: null,
    data: null,
    dataSourceErrors: [
      kind === 'status'
        ? 'live-status-reader-unconfigured'
        : 'v3-solvency-deployments-and-liability-plans-unconfigured',
    ],
    claims: {
      liveV3DeploymentVerified: false,
      onchainEvidencePresent: false,
      paymentSettlementEnabled: false,
    },
  };
  assertV3ReadEnvelope(body, kind);
  return Response.json(body, {
    status: 503,
    headers: { 'Cache-Control': 'no-store', 'X-Robots-Tag': 'noindex' },
  });
}

export function assertV3ReadEnvelope(value: unknown, kind: V3ReadKind): void {
  if (!value || typeof value !== 'object') throw new Error('V3 read envelope must be an object');
  const body = value as Record<string, unknown>;
  if (
    body.schemaVersion !== '1.0.0' ||
    body.schema !== `vaultfire.v3.protocol.${kind}.v1` ||
    body.protocolVersion !== '3.0.0' ||
    body.route !== `/api/v3/protocol/${kind}` ||
    body.status !== 'not-ready' ||
    body.readiness !== 'data-source-unconfigured' ||
    body.data !== null ||
    !Array.isArray(body.dataSourceErrors) ||
    body.dataSourceErrors.length !== 1 ||
    typeof body.observedAt !== 'string'
  ) {
    throw new Error('invalid V3 read envelope');
  }
}
