import { TaskTreeStore } from "./store.ts";
import { TaskTreeBackupService, taskTreeBackupConfigFromEnv } from "./backup.ts";

export function taskTreeRuntime(env: Record<string, string | undefined> = process.env) {
  const databaseUrl = env.TASKTREE_DATABASE_URL || env.DATABASE_URL || "";
  if (!databaseUrl) throw Object.assign(new Error("TASKTREE_DATABASE_URL or DATABASE_URL is required"), { code: "missing_database_url" });
  const store = new TaskTreeStore(databaseUrl);
  const backup = new TaskTreeBackupService(store, taskTreeBackupConfigFromEnv(env));
  return {
    store,
    backup,
    temporalAddress: env.TASKTREE_TEMPORAL_ADDRESS || env.TEMPORAL_ADDRESS || "",
    temporalNamespace: env.TASKTREE_TEMPORAL_NAMESPACE || "unidesk",
    taskQueue: env.TASKTREE_TEMPORAL_TASK_QUEUE || "hwlab-v03-tasktree"
  };
}
