/**
 * 环境变量配置
 * 从 process.env 读取，所有值必须有默认值或抛出错误
 */

// 数据库
export const DATABASE_URL =
    process.env.DATABASE_URL || "postgresql://localhost:5432/code";

// Redis
export const REDIS_URL = process.env.REDIS_URL || "redis://localhost:6379";
// Redis Cluster 节点列表（逗号分隔，用于 ioredis cluster 模式）
export const REDIS_CLUSTER_URLS = (process.env.REDIS_CLUSTER_URLS || "")
    .split(",")
    .map((u) => u.trim())
    .filter(Boolean);

// Session
export const ADMIN_SESSION_COOKIE_NAME =
    process.env.ADMIN_SESSION_COOKIE_NAME || "admin_session";
export const ADMIN_SESSION_TTL = parseInt(
    process.env.ADMIN_SESSION_TTL || "604800",
    10
); // 7 days

// 超级管理员（环境变量配置）
export const ADMIN_SUPER_USERNAME = process.env.ADMIN_SUPER_USERNAME || "";
export const ADMIN_SUPER_PASSWORD = process.env.ADMIN_SUPER_PASSWORD || "";
export const ADMIN_SUPER_PASSWORD_HASH =
    process.env.ADMIN_SUPER_PASSWORD_HASH || "";

// OIDC
export const OIDC_ENABLED = process.env.OIDC_ENABLED === "true";
export const OIDC_ISSUER = process.env.OIDC_ISSUER || "";
export const OIDC_CLIENT_ID = process.env.OIDC_CLIENT_ID || "";
export const OIDC_CLIENT_SECRET = process.env.OIDC_CLIENT_SECRET || "";
export const OIDC_REDIRECT_URI =
    process.env.OIDC_REDIRECT_URI ||
    "http://localhost:3000/api/auth/oidc/callback";

// Cookie 安全
export const COOKIE_SECURE = process.env.COOKIE_SECURE === "true";
export const COOKIE_SAME_SITE =
    (process.env.COOKIE_SAME_SITE as "strict" | "lax" | "none") || "lax";

// Rust 主应用集成
export const RUST_BACKEND_URL =
    process.env.RUST_BACKEND_URL || "http://localhost:3000";
export const ADMIN_API_SHARED_KEY =
    process.env.ADMIN_API_SHARED_KEY || "";

// adminrpc HTTP 服务地址（k8s 内部默认地址）
// 在 Kubernetes 环境中默认使用 Service DNS，在本地开发时覆盖为 localhost:9091
export const ADMIN_RPC_URL =
    process.env.ADMIN_RPC_URL || "http://adminrpc.gitdataai.svc.cluster.local:9091";
