/**
 * Resilience Constants
 * All "how do we recover from failure?" defaults.
 * Retry policies, backoff, rate limiting, circuit breaker thresholds.
 *
 * Note: import type avoids runtime circular dependency with ../resilience/
 * (constants/ provides values, resilience/ provides types).
 */

import type { RetryPolicy } from '../resilience/rate-limiter-types'

/** Max LLM call attempts (initial + retries). */
export const LLM_MAX_ATTEMPTS = 3

/** Exponential backoff delays between LLM retries. */
export const LLM_RETRY_DELAYS = [1_000, 2_000, 4_000] as const // 1s, 2s, 4s

/** Default retry policy for rate-limited operations. */
export const DEFAULT_RETRY_POLICY: RetryPolicy = {
  maxRetries: 3,
  initialDelayMs: 1_000,
  maxDelayMs: 10_000,
  jitterFactor: 0.3
}

/** Default rate limiter bucket capacity (requests per window). */
export const DEFAULT_RATE_LIMIT_CAPACITY = 300

/** Integration service rate limit: requests per org per integration per window. */
export const INTEGRATION_RATE_LIMIT_CAPACITY = 300

/** Integration service rate limit window. */
export const INTEGRATION_RATE_LIMIT_WINDOW_MS = 60_000 // 1min

/** Rate limiter refill window. */
export const DEFAULT_RATE_LIMIT_WINDOW_MS = 60_000 // 1min

/** Rate limiter: evict buckets inactive for N * refill interval. */
export const STALE_BUCKET_MULTIPLIER = 5

/** Circuit breaker: failures before opening. */
export const CIRCUIT_BREAKER_FAILURE_THRESHOLD = 5

/** Circuit breaker: cooldown before half-open probe. */
export const CIRCUIT_BREAKER_COOLDOWN_MS = 60_000 // 60s

/** Circuit breaker: successes in half-open to close. */
export const CIRCUIT_BREAKER_SUCCESS_THRESHOLD = 2

/** Circuit breaker: max concurrent requests in half-open state. */
export const CIRCUIT_BREAKER_HALF_OPEN_MAX_REQUESTS = 3
