/**
 * student-bff 下游 gRPC 服务配置.
 *
 * 仲裁依据:
 *   - coord-final-decisions §2 B2 (首次实现即 gRPC, 禁止 HTTP fetch)
 *   - coord-final-decisions §2 B8 (复用 shared-ts DownstreamClient 抽象)
 *   - president-final-rulings §2.4 (/readyz 探针按阶段扩展, env 控制启用)
 *   - port-allocation.md §5 (gRPC 端口分配)
 *
 * 阶段启用:
 *   - P3: iam (50052) + core-edu (50053) — 必需
 *   - P4: + content (50054) + data-ana (50055) — 必需
 *   - P5: + msg (50056) + ai (50058) — 必需
 *
 * proto 包名 (G17): next_edu_cloud.<domain>.v1
 */
import type { DownstreamServiceConfig } from "@edu/shared-ts/bff";
import { env } from "./env.js";

/**
 * student-bff 下游 gRPC 服务清单.
 *
 * enabled 字段控制是否启用 (P3 仅 iam + core-edu);
 * required 字段控制 /readyz 软失败策略 (president §2.4):
 *   - required=true: 失败返回 503, 触发 Pod 重启
 *   - required=false: 失败仅告警, 返回 200 + degraded=true
 *
 * MOCK_UPSTREAM=true 时所有服务返回 mock 数据, 不实际调用 gRPC.
 */
export const downstreamServices: DownstreamServiceConfig[] = [
  {
    name: "iam",
    grpcUrl: env.IAM_GRPC_URL,
    protoPath: "packages/shared-proto/proto/iam.proto",
    packageName: "next_edu_cloud.iam.v1",
    enabled: true,
    required: true,
  },
  {
    name: "core-edu",
    grpcUrl: env.CORE_EDU_GRPC_URL,
    protoPath: "packages/shared-proto/proto/core_edu.proto",
    packageName: "next_edu_cloud.core_edu.v1",
    enabled: true,
    required: true,
  },
  {
    name: "content",
    grpcUrl: env.CONTENT_GRPC_URL,
    protoPath: "packages/shared-proto/proto/content.proto",
    packageName: "next_edu_cloud.content.v1",
    enabled: true,
    required: true,
  },
  {
    name: "data-ana",
    grpcUrl: env.DATA_ANA_GRPC_URL,
    protoPath: "packages/shared-proto/proto/analytics.proto",
    packageName: "next_edu_cloud.analytics.v1",
    enabled: true,
    required: true,
  },
  {
    name: "msg",
    grpcUrl: env.MSG_GRPC_URL,
    protoPath: "packages/shared-proto/proto/msg.proto",
    packageName: "next_edu_cloud.msg.v1",
    enabled: true,
    required: true,
  },
  {
    name: "ai",
    grpcUrl: env.AI_GRPC_URL,
    protoPath: "packages/shared-proto/proto/ai.proto",
    packageName: "next_edu_cloud.ai.v1",
    enabled: true,
    required: true,
  },
];

/**
 * DownstreamClient 配置 (传给 shared-ts DownstreamClient).
 */
export const downstreamClientConfig = {
  mockUpstream: env.MOCK_UPSTREAM,
  devMode: env.DEV_MODE,
  services: downstreamServices,
  defaultTimeoutMs: env.DOWNSTREAM_TIMEOUT_MS,
  defaultRetryCount: env.DOWNSTREAM_RETRY_COUNT,
  defaultRetryBackoffMs: env.DOWNSTREAM_RETRY_BACKOFF_MS,
};
