import { Hono } from "hono";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import type { CliLauncher } from "./cli-launcher.js";
import type { WsBridge } from "./ws-bridge.js";
import type { SessionStore } from "./session-store.js";
import type { WorktreeTracker } from "./worktree-tracker.js";
import type { TerminalManager } from "./terminal-manager.js";
import { registerFsRoutes } from "./routes/fs-routes.js";
import { registerSkillRoutes } from "./routes/skills-routes.js";
import { registerEnvRoutes } from "./routes/env-routes.js";
import { registerCronRoutes } from "./routes/cron-routes.js";
import { registerAgentRoutes } from "./routes/agent-routes.js";
import { registerPromptRoutes } from "./routes/prompt-routes.js";
import { registerSettingsRoutes } from "./routes/settings-routes.js";
import { registerGitRoutes } from "./routes/git-routes.js";
import { registerSystemRoutes } from "./routes/system-routes.js";
import { registerHistoryRoutes } from "./routes/history-routes.js";
import { registerTemplateRoutes } from "./routes/template-routes.js";
import { registerLinearRoutes } from "./routes/linear-routes.js";
import { registerSessionRoutes } from "./routes/session-routes.js";
import { registerQueueRoutes } from "./routes/queue-routes.js";
import { registerRecordingRoutes } from "./routes/recording-routes.js";
import { registerContainerRoutes } from "./routes/container-routes.js";
import { registerEditorRoutes } from "./routes/editor-routes.js";
import { registerQualityGateRoutes } from "./routes/quality-gate-routes.js";
import { registerStatsRoutes } from "./routes/stats-routes.js";
import { registerSearchRoutes } from "./routes/search-routes.js";
import { registerPushRoutes } from "./routes/push-routes.js";

const UPDATE_CHECK_STALE_MS = 5 * 60 * 1000;
const ROUTES_DIR = dirname(fileURLToPath(import.meta.url));
const WEB_DIR = dirname(ROUTES_DIR);

// ─── Route Dependencies ───────────────────────────────────────────

export interface RouteDependencies {
  launcher: CliLauncher;
  wsBridge: WsBridge;
  sessionStore: SessionStore;
  worktreeTracker: WorktreeTracker;
  terminalManager: TerminalManager;
  prPoller?: import("./pr-poller.js").PRPoller;
  recorder?: import("./recorder.js").RecorderManager;
  cronScheduler?: import("./cron-scheduler.js").CronScheduler;
  agentExecutor?: import("./agent-executor.js").AgentExecutor;
}

export function createRoutes(deps: RouteDependencies) {
  const {
    launcher,
    wsBridge,
    sessionStore,
    worktreeTracker,
    terminalManager,
    prPoller,
    recorder,
    cronScheduler,
    agentExecutor,
  } = deps;
  const api = new Hono();

  // ─── Domain route modules ─────────────────────────────────────

  registerSessionRoutes(api, {
    launcher,
    wsBridge,
    sessionStore,
    worktreeTracker,
    prPoller,
  });
  registerQueueRoutes(api, { wsBridge });
  registerEditorRoutes(api, launcher);
  registerRecordingRoutes(api, recorder);
  registerContainerRoutes(api);
  registerQualityGateRoutes(api, { wsBridge });

  // ─── Existing domain route modules ────────────────────────────

  registerFsRoutes(api, {
    getSessionCwds: () => wsBridge.getAllSessions().map((s) => s.cwd).filter(Boolean),
  });
  registerEnvRoutes(api, { webDir: WEB_DIR });
  registerPromptRoutes(api);
  registerSettingsRoutes(api);
  registerTemplateRoutes(api);
  registerLinearRoutes(api);
  registerGitRoutes(api, prPoller);
  registerSystemRoutes(api, {
    launcher,
    wsBridge,
    terminalManager,
    updateCheckStaleMs: UPDATE_CHECK_STALE_MS,
  });
  registerSkillRoutes(api);
  registerCronRoutes(api, cronScheduler);
  registerAgentRoutes(api, agentExecutor);
  registerHistoryRoutes(api, sessionStore);
  registerStatsRoutes(api, sessionStore);
  registerSearchRoutes(api, sessionStore);
  registerPushRoutes(api);

  return api;
}
