import { initializeAllContainers, registerContainers } from '@bytelyst/cosmos';
import type { ContainerConfig } from '@bytelyst/cosmos';
import { config } from './config.js';

const CONTAINER_DEFS: Record<string, ContainerConfig> = {
  products: { partitionKeyPath: '/id' },
  users: { partitionKeyPath: '/id' },
  settings: { partitionKeyPath: '/userId' },
  devices: { partitionKeyPath: '/userId' },
  notification_prefs: { partitionKeyPath: '/userId' },
  audit_log: { partitionKeyPath: '/category', defaultTtl: 90 * 86400 },
  feature_flags: { partitionKeyPath: '/id' },
  // Growth modules
  invitation_codes: { partitionKeyPath: '/id' },
  referrals: { partitionKeyPath: '/id' },
  referrals_v2: { partitionKeyPath: '/referrerId' },
  // Billing modules
  subscriptions: { partitionKeyPath: '/userId' },
  payments: { partitionKeyPath: '/userId' },
  licenses: { partitionKeyPath: '/id' },
  plans: { partitionKeyPath: '/id' },
  usage_daily: { partitionKeyPath: '/userId' },
  // API tokens
  api_tokens: { partitionKeyPath: '/id' },
  rate_limit_entries: { partitionKeyPath: '/id', defaultTtl: 24 * 3600 },
  // Tracker modules
  tracker_items: { partitionKeyPath: '/id' },
  comments: { partitionKeyPath: '/itemId' },
  votes: { partitionKeyPath: '/itemId' },
  // Themes
  themes: { partitionKeyPath: '/id' },
  // Waitlist (pre-launch signups)
  waitlist: { partitionKeyPath: '/email' },
  // Sessions (refresh token rotation + device tracking)
  sessions: { partitionKeyPath: '/userId', defaultTtl: 30 * 86400 },
  // Email/push delivery log
  delivery_log: { partitionKeyPath: '/pk', defaultTtl: 90 * 86400 },
  // Status page incidents
  incidents: { partitionKeyPath: '/productId' },
  // Password reset + email verification
  password_reset_tokens: { partitionKeyPath: '/productId', defaultTtl: 86400 },
  email_verifications: { partitionKeyPath: '/productId', defaultTtl: 7 * 86400 },
  // SmartAuth — OAuth provider linking
  auth_providers: { partitionKeyPath: '/userId' },
  // SmartAuth — Enterprise IdP configs
  auth_enterprise_idps: { partitionKeyPath: '/orgId' },
  // SmartAuth — TOTP MFA secrets + recovery codes
  auth_mfa: { partitionKeyPath: '/userId' },
  // SmartAuth — MFA enforcement policies (per product)
  auth_mfa_policies: { partitionKeyPath: '/productId' },
  // SmartAuth — WebAuthn passkeys
  auth_passkeys: { partitionKeyPath: '/userId' },
  // SmartAuth — Device trust + fingerprinting
  auth_devices: { partitionKeyPath: '/userId' },
  // SmartAuth — Login events (audit trail, 365-day TTL)
  auth_login_events: { partitionKeyPath: '/userId', defaultTtl: 365 * 86400 },
  // IP allow/deny rules
  ip_rules: { partitionKeyPath: '/productId' },
  // Data exports
  export_jobs: { partitionKeyPath: '/productId', defaultTtl: 30 * 86400 },
  // Maintenance windows
  maintenance_windows: { partitionKeyPath: '/productId' },
  // Scheduled jobs
  job_definitions: { partitionKeyPath: '/productId' },
  job_runs: { partitionKeyPath: '/pk' },
  // Generic orchestration runs
  agent_runs: { partitionKeyPath: '/productId', defaultTtl: 30 * 86400 },
  agent_run_steps: { partitionKeyPath: '/pk', defaultTtl: 30 * 86400 },
  // Cross-product personal timeline
  timeline_items: { partitionKeyPath: '/productId', defaultTtl: 90 * 86400 },
  // Canonical tenant model
  organizations: { partitionKeyPath: '/productId' },
  workspaces: { partitionKeyPath: '/orgId' },
  org_memberships: { partitionKeyPath: '/orgId' },
  // Human review / approval queue
  review_queue: { partitionKeyPath: '/productId', defaultTtl: 30 * 86400 },
  // Agent registry and versioned prompt/config definitions
  agent_registry: { partitionKeyPath: '/productId' },
  agent_versions: { partitionKeyPath: '/agentId' },
  // Agent governance / evaluations
  agent_evaluation_suites: { partitionKeyPath: '/productId' },
  agent_evaluation_cases: { partitionKeyPath: '/suiteId' },
  agent_evaluation_runs: { partitionKeyPath: '/productId', defaultTtl: 30 * 86400 },
  agent_evaluation_results: { partitionKeyPath: '/runId', defaultTtl: 30 * 86400 },
  // AI budget and cost governance
  ai_budget_policies: { partitionKeyPath: '/productId' },
  ai_budget_spend_entries: { partitionKeyPath: '/productId', defaultTtl: 30 * 86400 },
  ai_budget_alerts: { partitionKeyPath: '/productId', defaultTtl: 30 * 86400 },
  // Shared knowledge / retrieval catalog
  knowledge_bases: { partitionKeyPath: '/productId' },
  knowledge_sources: { partitionKeyPath: '/knowledgeBaseId' },
  knowledge_chunks: { partitionKeyPath: '/knowledgeBaseId', defaultTtl: 90 * 86400 },
  // Enterprise provisioning / SCIM
  scim_connectors: { partitionKeyPath: '/orgId' },
  scim_user_sync: { partitionKeyPath: '/connectorId' },
  scim_group_sync: { partitionKeyPath: '/connectorId' },
  scim_events: { partitionKeyPath: '/connectorId', defaultTtl: 90 * 86400 },
  // Support case management
  support_cases: { partitionKeyPath: '/productId' },
  support_case_notes: { partitionKeyPath: '/caseId' },
  support_case_escalations: { partitionKeyPath: '/caseId', defaultTtl: 90 * 86400 },
  // Telemetry (client diagnostics — see docs/WINDSURF/CLIENT_TELEMETRY_DESIGN.md)
  telemetry_events: { partitionKeyPath: '/pk', defaultTtl: 30 * 86400 },
  telemetry_error_clusters: { partitionKeyPath: '/pk', defaultTtl: 90 * 86400 },
  telemetry_collection_policies: { partitionKeyPath: '/productId' },
  // Database migrations tracking
  migrations: { partitionKeyPath: '/productId' },
  // Webhook subscriptions + delivery log
  webhook_subscriptions: { partitionKeyPath: '/productId' },
  webhook_deliveries: { partitionKeyPath: '/pk', defaultTtl: 30 * 86400 },
  // Generic Marketplace
  marketplace_listings: { partitionKeyPath: '/productId' },
  marketplace_reviews: { partitionKeyPath: '/listingId' },
  marketplace_installs: { partitionKeyPath: '/userId' },
  marketplace_certifications: { partitionKeyPath: '/listingId' },
  marketplace_reports: { partitionKeyPath: '/listingId' },
  marketplace_votes: { partitionKeyPath: '/listingId' },
  // P2 — Product Intelligence
  experiments: { partitionKeyPath: '/id' },
  experiment_assignments: { partitionKeyPath: '/experimentId' },
  // A/B Testing — Extended containers for Intelligent A/B Testing
  ab_testing_variants: { partitionKeyPath: '/experimentId' },
  ab_testing_events: { partitionKeyPath: '/experimentId', defaultTtl: 90 * 86400 },
  ab_testing_metrics: { partitionKeyPath: '/experimentId' },
  experiment_suggestions: { partitionKeyPath: '/productId', defaultTtl: 180 * 86400 },
  analytics_rollups: { partitionKeyPath: '/productId' },
  feedback: { partitionKeyPath: '/productId' },
  impersonation_sessions: { partitionKeyPath: '/productId', defaultTtl: 90 * 86400 },
  changelog: { partitionKeyPath: '/productId' },
  // Remote Diagnostics (see docs/devops/REMOTE_DIAGNOSTICS_ROADMAP.md)
  debug_sessions: { partitionKeyPath: '/id', defaultTtl: 7 * 86400 },
  debug_traces: { partitionKeyPath: '/pk', defaultTtl: 7 * 86400 },
  debug_logs: { partitionKeyPath: '/pk', defaultTtl: 3 * 86400 },
  debug_screenshots: { partitionKeyPath: '/sessionId', defaultTtl: 7 * 86400 },
  diagnostic_triggers: { partitionKeyPath: '/id' },
  session_replays: { partitionKeyPath: '/pk', defaultTtl: 7 * 86400 },
  performance_profiles: { partitionKeyPath: '/pk', defaultTtl: 7 * 86400 },
  // Predictive Analytics
  user_features: { partitionKeyPath: '/userId', defaultTtl: 90 * 86400 },
  product_health: { partitionKeyPath: '/productId' },
  feature_definitions: { partitionKeyPath: '/productId' },
  churn_predictions: { partitionKeyPath: '/userId', defaultTtl: 120 * 86400 },
  retention_campaigns: { partitionKeyPath: '/productId' },
  campaign_deliveries: { partitionKeyPath: '/userId', defaultTtl: 90 * 86400 },
  model_performance: { partitionKeyPath: '/id' },
  // AI Diagnostics (see docs/roadmaps/AI_DIAGNOSTIC_ASSISTANT_ROADMAP.md)
  error_clusters: { partitionKeyPath: '/productId', defaultTtl: 90 * 86400 },
  error_fingerprints: { partitionKeyPath: '/fingerprintHash' },
  diagnostic_insights: { partitionKeyPath: '/clusterId', defaultTtl: 90 * 86400 },
  diagnostic_queries: { partitionKeyPath: '/userId', defaultTtl: 30 * 86400 },
  proactive_alerts: { partitionKeyPath: '/productId', defaultTtl: 30 * 86400 },
  // Onboarding analytics (Phase 4.3)
  onboarding_events: { partitionKeyPath: '/productId' },
  onboarding_completions: { partitionKeyPath: '/productId' },
  // Broadcast Messaging & Surveys (see docs/roadmaps/not-started/platform_BROADCAST_SURVEY_ROADMAP.md)
  broadcasts: { partitionKeyPath: '/productId' },
  broadcast_deliveries: { partitionKeyPath: '/userId', defaultTtl: 90 * 86400 },
  broadcast_reads: { partitionKeyPath: '/userId', defaultTtl: 90 * 86400 },
  in_app_messages: { partitionKeyPath: '/userId', defaultTtl: 30 * 86400 },
  surveys: { partitionKeyPath: '/productId' },
  survey_responses: { partitionKeyPath: '/surveyId', defaultTtl: 365 * 86400 },
  user_survey_states: { partitionKeyPath: '/userId', defaultTtl: 90 * 86400 },
  // CDN Pipeline (P2)
  cdn_assets: { partitionKeyPath: '/productId' },
  cdn_purge_requests: { partitionKeyPath: '/productId', defaultTtl: 90 * 86400 },
  cdn_origin_configs: { partitionKeyPath: '/productId' },
  // Full-Text Search (P2)
  search_index: { partitionKeyPath: '/productId' },
  search_suggestions: { partitionKeyPath: '/productId', defaultTtl: 180 * 86400 },
  // Billing Dunning (P2)
  dunning_campaigns: { partitionKeyPath: '/productId' },
  dunning_policies: { partitionKeyPath: '/productId' },
  // Multi-Tenant (P3)
  tenants: { partitionKeyPath: '/productId' },
  tenant_members: { partitionKeyPath: '/tenantId' },
  tenant_invites: { partitionKeyPath: '/tenantId', defaultTtl: 7 * 86400 },
  // Data Retention (P3)
  retention_policies: { partitionKeyPath: '/productId' },
  retention_jobs: { partitionKeyPath: '/productId', defaultTtl: 365 * 86400 },
  // Backup/Restore (P3)
  backups: { partitionKeyPath: '/productId' },
  restores: { partitionKeyPath: '/productId' },
  backup_configs: { partitionKeyPath: '/productId' },
  // API Versioning (P3)
  api_versions: { partitionKeyPath: '/productId' },
  api_version_pins: { partitionKeyPath: '/productId' },
  // i18n (P3)
  translations: { partitionKeyPath: '/locale' },
  i18n_locales: { partitionKeyPath: '/locale' },
  // Agent Gigafactory — fleet coordinator (see modules/fleet/README.md)
  fleet_jobs: {
    partitionKeyPath: '/productId',
    // listJobs() orders by (priorityOrder, createdAt) — Azure Cosmos requires a
    // matching composite index to serve multi-field ORDER BY (e.g. fleetMetrics).
    compositeIndexes: [
      [
        { path: '/priorityOrder', order: 'ascending' },
        { path: '/createdAt', order: 'ascending' },
      ],
    ],
  },
  fleet_runs: { partitionKeyPath: '/jobId' },
  // TTL backstop (2 days) for lease docs: a HELD lease is renewed continuously so
  // its _ts stays fresh and it never expires while active; only finished
  // (released/expired) leases — which stop being written — age out. This is purely
  // defense-in-depth behind the app-level reaper/GC (which deletes finished leases
  // after ~24h and owns recovery: requeue + epoch bump + checkpoint). NOTE: TTL is
  // deliberately NOT set on fleet_events (event ids are `<jobId>:evt:<seq>` where
  // seq = current count, so partial TTL deletion could collide ids) — events/runs/
  // jobs are pruned by the cascade GC instead.
  fleet_leases: { partitionKeyPath: '/jobId', defaultTtl: 2 * 86400 },
  fleet_factories: { partitionKeyPath: '/productId' },
  fleet_profiles: { partitionKeyPath: '/productId' },
  fleet_events: { partitionKeyPath: '/jobId' },
  fleet_artifacts: { partitionKeyPath: '/jobId' },
  fleet_factory_tokens: { partitionKeyPath: '/productId' },
  // M0 RU gate: per-product monotonic "work changed" counter (see
  // docs/GIGAFACTORY/FLEET_DISPATCH_REDESIGN.md §8/§12).
  fleet_queue_state: { partitionKeyPath: '/productId' },
};

export async function initCosmosIfNeeded(): Promise<void> {
  registerContainers(CONTAINER_DEFS);

  const shouldInit = config.NODE_ENV !== 'production' || process.env.COSMOS_AUTO_INIT === 'true';
  if (!shouldInit) return;

  try {
    await initializeAllContainers();
    process.stdout.write('[platform-service] Cosmos containers ensured\n');
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err);
    process.stderr.write(`[platform-service] Cosmos init failed: ${msg}\n`);
  }
}
