/**
 * SSH runtime configuration. Feature is disabled by default — set
 * `ssh.enabled: true` in config.yaml to expose SshExec/Upload/Download
 * tools and the SSH connections UI.
 *
 * Plan: docs/superpowers/plans/2026-05-12-ssh-tool-integration.md
 * Phase 0: feature flag scaffolding only. Subsequent phases wire up
 * the DB schema, repos, session core, HTTP layer, UI, and tool registration.
 */

/**
 * Interactive SSH Console (live PTY-backed shell session).
 *
 * Plan: docs/superpowers/plans/2026-05-13-ssh-console.md
 * Disabled by default. When enabled, exposes SshConsole* tools and the
 * tasks-side Terminal tab. Session lifecycle, scrollback, and AI-input
 * caps are bounded by these knobs.
 */
export interface SshConsoleConfig {
  enabled: boolean;
  /** I/O-less seconds before auto-close (both human and AI input count as activity). */
  idleTimeoutSeconds: number;
  /** Hard wall-clock cap for a single session. */
  maxSessionDurationSeconds: number;
  /** Per-session scrollback retained in memory (raw PTY bytes). */
  scrollbackBytes: number;
  /** Cap concurrent live sessions per connection (eviction order: oldest first). */
  maxSessionsPerConnection: number;
  /** Cap a single SshConsoleSend payload (bytes). */
  maxInputBytesPerSend: number;
  /** How many trailing screen lines to auto-inject into the LLM prompt after AI input. */
  autoInjectScreenLines: number;
  /** Default PTY width when the client hasn't sent a resize yet. */
  defaultCols: number;
  /** Default PTY height when the client hasn't sent a resize yet. */
  defaultRows: number;
}

export interface SshRuntimeConfig {
  enabled: boolean;

  /**
   * When true, allow SSH connections to resolve to private/loopback addresses.
   * Same semantics as `mcp.allow_private_addresses`. Required for self-hosted
   * targets on a LAN. Admin can grant per-connection exceptions on globals.
   */
  allowPrivateAddresses: boolean;

  callTimeoutSeconds: number;
  maxOutputBytes: number;
  maxUploadSizeMb: number;
  maxDownloadSizeMb: number;

  /** How long to keep ssh_audit_log rows. Admin can prune via UI. */
  auditRetentionDays: number;

  /** When true, admin can use any connection without an explicit grant (audited regardless). */
  adminBypassesGrants: boolean;

  /** Abuse detection — failure window length, threshold, and lock duration. */
  abuseWindowMinutes: number;
  abuseFailureThreshold: number;
  abuseLockMinutes: number;

  /** Interactive SSH Console nested config. See SshConsoleConfig. */
  console: SshConsoleConfig;
}

export const SSH_CONSOLE_DEFAULTS: SshConsoleConfig = {
  enabled: false,
  idleTimeoutSeconds: 1800,
  maxSessionDurationSeconds: 14400,
  scrollbackBytes: 524288,
  maxSessionsPerConnection: 3,
  maxInputBytesPerSend: 16384,
  autoInjectScreenLines: 24,
  defaultCols: 120,
  defaultRows: 32,
};

export const SSH_DEFAULTS: SshRuntimeConfig = {
  enabled: false,
  allowPrivateAddresses: false,
  callTimeoutSeconds: 30,
  maxOutputBytes: 32 * 1024,
  maxUploadSizeMb: 100,
  maxDownloadSizeMb: 100,
  auditRetentionDays: 90,
  adminBypassesGrants: true,
  abuseWindowMinutes: 10,
  abuseFailureThreshold: 5,
  abuseLockMinutes: 30,
  console: { ...SSH_CONSOLE_DEFAULTS },
};

export function mergeSshConfig(partial: Partial<SshRuntimeConfig> | undefined): SshRuntimeConfig {
  const consolePartial = (partial?.console ?? {}) as Partial<SshConsoleConfig>;
  return {
    ...SSH_DEFAULTS,
    ...(partial ?? {}),
    console: { ...SSH_CONSOLE_DEFAULTS, ...consolePartial },
  };
}
