/*
 * SPEC: PJ2026-010403 API契约 draft-2026-06-17-r0; PJ2026-0102 Agent编排 draft-2026-06-17-r0; PJ2026-0104010803 Workbench唯一投影 draft-2026-06-19-p1-agentrun-incremental-cursor; draft-2026-06-20-p1-zero-split-durable-realtime; draft-2026-06-24-p0-aggregate-event-stream; PJ2026-010205 HWLAB接入 draft-2026-06-25-p0-session-warm-runner-contract.
 * 职责: Cloud runtime store public factories and compatibility exports.
 */

import {
  RUNTIME_ADAPTER_ENV,
  RUNTIME_DURABLE_ENV,
  RUNTIME_STORE_KIND_POSTGRES,
  normalizeRuntimeAdapter
} from "./runtime-store-core.ts";
import { CloudRuntimeStore } from "./runtime-store-memory.ts";
import { PostgresCloudRuntimeStore } from "./runtime-store-postgres.ts";

export {
  RUNTIME_ADAPTER_ENV,
  RUNTIME_DB_POOL_MAX_ENV,
  RUNTIME_DURABILITY_REQUIRED_EVIDENCE,
  RUNTIME_DURABLE_ADAPTER_AUTH_BLOCKED,
  RUNTIME_DURABLE_ADAPTER_DRIVER_MISSING,
  RUNTIME_DURABLE_ADAPTER_MIGRATION_BLOCKED,
  RUNTIME_DURABLE_ADAPTER_MISSING,
  RUNTIME_DURABLE_ADAPTER_QUERY_BLOCKED,
  RUNTIME_DURABLE_ADAPTER_SCHEMA_BLOCKED,
  RUNTIME_DURABLE_ADAPTER_SSL_BLOCKED,
  RUNTIME_DURABLE_ADAPTER_UNCONFIGURED,
  RUNTIME_DURABLE_ENV,
  RUNTIME_STORE_KIND,
  RUNTIME_STORE_KIND_POSTGRES,
  buildPostgresPoolConfig,
  classifyRuntimeDbError
} from "./runtime-store-core.ts";
export { CloudRuntimeStore } from "./runtime-store-memory.ts";
export { PostgresCloudRuntimeStore } from "./runtime-store-postgres.ts";

export function createCloudRuntimeStore(options = {}) {
  return new CloudRuntimeStore(options);
}

export function createConfiguredCloudRuntimeStore(options = {}) {
  const env = options.env ?? process.env;
  const adapter = normalizeRuntimeAdapter(options.adapter ?? env?.[RUNTIME_ADAPTER_ENV]);
  const durableRequested =
    options.durable === true ||
    env?.[RUNTIME_DURABLE_ENV] === "1" ||
    env?.[RUNTIME_DURABLE_ENV] === "true";

  if (adapter === RUNTIME_STORE_KIND_POSTGRES || durableRequested) {
    return new PostgresCloudRuntimeStore({
      ...options,
      env,
      dbUrl: options.dbUrl ?? env?.HWLAB_CLOUD_DB_URL,
      sslMode: options.sslMode ?? env?.HWLAB_CLOUD_DB_SSL_MODE
    });
  }

  return createCloudRuntimeStore(options);
}
