import { CONFIG_DEFAULTS } from '../config/config-defaults';
import { OptimizationPolicy } from '../types/constants';

/**
 * Memory Partition/Sort Key prefixes.
 */
export const MEMORY_KEYS = {
  CONVERSATION_PREFIX: 'CONV#',
  FACT_PREFIX: 'FACT#',
  LESSON_PREFIX: 'LESSON#',
  SUMMARY_PREFIX: 'SUMMARY#',
  STRATEGIC_REVIEW: 'SYSTEM#STRATEGIC_REVIEW',
  REPUTATION_PREFIX: 'REPUTATION#',
  WORKSPACE_PREFIX: 'WORKSPACE#',
  ORG_PREFIX: 'ORG#',
  HEALTH_PREFIX: 'HEALTH#',
  TRACK_PREFIX: 'TRACK#',
  GAP_LOCK_PREFIX: 'GAP_LOCK#',
  WARM_PREFIX: 'WARM#',
  DISTILLED_PREFIX: 'DISTILLED#',
  GAP_PREFIX: 'GAP#',
  SESSIONS_PREFIX: 'SESSIONS#',
  FAILED_PLAN_PREFIX: 'FAILED_PLAN#',
  CODER_PREFIX: 'CODER#',
  PLANNER_PREFIX: 'PLANNER#',
  REFLECTOR_PREFIX: 'REFLECTOR#',
  RECOVERY_PREFIX: 'RECOVERY#',
  EVOLUTION_PREFIX: 'EVOLUTION#PENDING#',
  HANDOFF_PREFIX: 'HANDOFF#',
  SAFETY_VIOLATION_PREFIX: 'SAFETY#VIOLATION#',
  SAFETY_BLAST_RADIUS_PREFIX: 'SAFETY#BLAST_RADIUS#',
  SYSTEM_PREFIX: 'SYSTEM#',
} as const;

/**
 * Retention policies (days).
 * Now centralized through CONFIG_DEFAULTS for hot-swappability.
 */
export const RETENTION = {
  MESSAGES_DAYS: CONFIG_DEFAULTS?.MESSAGE_RETENTION_DAYS?.code ?? 30,
  TRACES_DAYS: CONFIG_DEFAULTS?.TRACE_RETENTION_DAYS?.code ?? 30,
  FACTS_DAYS: CONFIG_DEFAULTS?.FACTS_RETENTION_DAYS?.code ?? 365,
  LESSONS_DAYS: CONFIG_DEFAULTS?.LESSONS_RETENTION_DAYS?.code ?? 90,
  SUMMARY_DAYS: CONFIG_DEFAULTS?.SUMMARY_RETENTION_DAYS?.code ?? 30,
  GAPS_DAYS: CONFIG_DEFAULTS?.GAPS_RETENTION_DAYS?.code ?? 60,
  REPUTATION_DAYS: CONFIG_DEFAULTS?.REPUTATION_RETENTION_DAYS?.code ?? 365,
  SESSION_METADATA_DAYS: 90,
  EPHEMERAL_DAYS: 1,
  HEALTH_DAYS: 7,
  MAX_PINNED_SESSION_DAYS: 365,
} as const;

/**
 * Cache TTL policies (milliseconds).
 * Centralized through CONFIG_DEFAULTS for hot-swappability.
 */
export const CACHE_TTL = {
  USER_DATA: CONFIG_DEFAULTS?.CACHE_TTL_USER_DATA_MS?.code ?? 300000,
  CONVERSATION: CONFIG_DEFAULTS?.CACHE_TTL_CONVERSATION_MS?.code ?? 120000,
  GLOBAL: CONFIG_DEFAULTS?.CACHE_TTL_GLOBAL_MS?.code ?? 900000,
  SEARCH: CONFIG_DEFAULTS?.CACHE_TTL_SEARCH_MS?.code ?? 180000,
} as const;

/**
 * Resource Limits.
 */
export const LIMITS = {
  MAX_CONTEXT_LENGTH: 32768,
  MAX_MESSAGES: 100,
  STALE_GAP_DAYS: CONFIG_DEFAULTS?.STALE_GAP_DAYS?.code ?? 30,
  TRACE_TRUNCATE_LENGTH: 2000,
  DEFAULT_LOCK_TTL: (CONFIG_DEFAULTS?.RECOVERY_LOCK_TTL_SECONDS?.code ?? 1200) / 3,
  TWO_YEARS_DAYS: 730,
  DEFAULT_QUERY_LIMIT: 100,
  BATCH_SIZE_SMALL: 10,
  BATCH_SIZE_MEDIUM: 25,
  BATCH_SIZE_LARGE: 50,
} as const;

export const PAGINATION = {
  DEFAULT_LIMIT: 100,
  TRACE_DELETE_LIMIT: 50,
  TRACE_QUERY_LIMIT: 10,
  COLLABORATION_QUERY_LIMIT: 20,
} as const;

/**
 * Optimization Policies.
 */
export const OPTIMIZATION_POLICIES = {
  AGGRESSIVE: OptimizationPolicy.AGGRESSIVE,
  CONSERVATIVE: OptimizationPolicy.CONSERVATIVE,
  BALANCED: OptimizationPolicy.BALANCED,
} as const;

/**
 * Default values for system insights and gap analysis.
 */
export const INSIGHT_DEFAULTS = {
  CONFIDENCE: 9,
  IMPACT: 6,
  COMPLEXITY: 4,
  RISK: 2,
  URGENCY: 5,
  PRIORITY: 5,
} as const;
