// [TSM.ID].[11031972] -- All Rights Reserved. Proprietary & Confidential.
// XCU Dynamic Module Registry — Fetches from xcu_iam PostgreSQL via API Gateway
// Fallback to static if API offline

let _cachedRegistry = null;

// Dynamic fetch from XCU API Gateway
export async function fetchModuleRegistry() {
  if (_cachedRegistry) return _cachedRegistry;
  try {
    const apiBase = import.meta.env.VITE_XCU_API_URL || '';
    const res = await fetch(`${apiBase}/api/v1/modules`);
    if (!res.ok) throw new Error(`HTTP ${res.status}`);
    _cachedRegistry = await res.json();
    console.log('[XCU REGISTRY] Loaded from API Gateway (xcu_iam PostgreSQL)');
    return _cachedRegistry;
  } catch (err) {
    console.warn('[XCU REGISTRY] API offline, using static fallback:', err.message);
    return STATIC_MODULE_REGISTRY;
  }
}

// Invalidate cache (call after module CRUD)
export function invalidateModuleCache() {
  _cachedRegistry = null;
}

// Static fallback (hardcoded 99 modules for offline resilience)
export const STATIC_MODULE_REGISTRY = {
  group1: {
    name: 'KELOMPOK I: THE FOUNDATION (Modul 1-15)',
    modules: [
      { id: 'm1', name: 'Cerberus Matrix (Modul 1)' },
      { id: 'm2', name: 'Zero-Copy Routing Core Alpha (Modul 2)' },
      { id: 'm3', name: 'Zero-Copy Routing Core Beta (Modul 3)' },
      { id: 'm4', name: 'Zero-Copy Routing Core Gamma (Modul 4)' },
      { id: 'm5', name: 'Zero-Copy Routing Core Omega (Modul 5)' },
      { id: 'm6', name: 'Bitrate Optimizer I (Modul 6)' },
      { id: 'm7', name: 'Bitrate Optimizer II (Modul 7)' },
      { id: 'm8', name: 'Bitrate Optimizer III (Modul 8)' },
      { id: 'm9', name: 'Bitrate Optimizer IV (Modul 9)' },
      { id: 'm10', name: 'Bitrate Optimizer V (Modul 10)' },
      { id: 'm11', name: 'Thread Pinning Alpha (Modul 11)' },
      { id: 'm12', name: 'Thread Pinning Beta (Modul 12)' },
      { id: 'm13', name: 'Thread Pinning Gamma (Modul 13)' },
      { id: 'm14', name: 'Thread Pinning Delta (Modul 14)' },
      { id: 'm15', name: 'Neural WebTransport Matrix (Modul 15)' }
    ]
  },
  group2: {
    name: 'KELOMPOK II: SECURITY & AUTO-OPS (Modul 16-29)',
    modules: [
      { id: 'm16', name: 'Auto-TLS Matrix (Modul 16)' },
      { id: 'm17', name: 'XChaCha20 Core Sender (Modul 17)' },
      { id: 'm18', name: 'XChaCha20 Core Receiver (Modul 18)' },
      { id: 'm19', name: 'The Scribe - Audio Transcriber (Modul 19)' },
      { id: 'm20', name: 'Bandwidth Throttle Gate 1 (Modul 20)' },
      { id: 'm21', name: 'Bandwidth Throttle Gate 2 (Modul 21)' },
      { id: 'm22', name: 'Bandwidth Throttle Gate 3 (Modul 22)' },
      { id: 'm23', name: 'Bandwidth Throttle Gate 4 (Modul 23)' },
      { id: 'm24', name: 'Bandwidth Throttle Gate 5 (Modul 24)' },
      { id: 'm25', name: 'X-Grid CRDT Protocol (Modul 25)' },
      { id: 'm26', name: 'X-Grid State Reconciliation (Modul 26)' },
      { id: 'm27', name: 'X-Grid Gossip Mesh (Modul 27)' },
      { id: 'm28', name: 'The Inquisitor AI (Modul 28)' },
      { id: 'm29', name: 'The Inquisitor Policy Enforcer (Modul 29)' }
    ]
  },
  group3: {
    name: 'KELOMPOK III: THE IMPOSSIBLE PHYSICS (Modul 30-45)',
    modules: [
      { id: 'm30', name: 'Orbital Dark-Site (Modul 30)' },
      { id: 'm31', name: 'Orbital Vicon Converter (Modul 31)' },
      { id: 'm32', name: 'Lazarus Matrix Voice Cloning (Modul 32)' },
      { id: 'm33', name: 'Cassandra Matrix Deepfake (Modul 33)' },
      { id: 'm34', name: 'Thermo Matrix Dyson Sensor (Modul 34)' },
      { id: 'm35', name: 'Thermo Matrix Cooling (Modul 35)' },
      { id: 'm36', name: 'Doppler Sonar AirDrop (Modul 36)' },
      { id: 'm37', name: 'Phantom Matrix Steganography (Modul 37)' },
      { id: 'm38', name: 'Phantom Matrix Decoder (Modul 38)' },
      { id: 'm39', name: 'Aegis Forensic Watermark (Modul 39)' },
      { id: 'm40', name: 'Aegis Threat Quarantine (Modul 40)' },
      { id: 'm41', name: 'GSM / HT Node Alpha (Modul 41)' },
      { id: 'm42', name: 'GSM / HT Node Beta (Modul 42)' },
      { id: 'm43', name: 'The Pulsar Matrix (Modul 43)' },
      { id: 'm44', name: 'Biomechanical Resonance Matrix (Modul 44)' },
      { id: 'm45', name: 'Tesseract Hologram Migration (Modul 45)' }
    ]
  },
  group4: {
    name: 'KELOMPOK IV: CYBER WARFARE (Modul 46-67)',
    modules: [
      { id: 'm46', name: 'Eclipse DPI Disguiser (Modul 46)' },
      { id: 'm47', name: 'Eclipse DPI Payload Encoder (Modul 47)' },
      { id: 'm48', name: 'Eclipse DPI Traffic Shaper (Modul 48)' },
      { id: 'm49', name: 'Vanguard Firewall OS (Modul 49)' },
      { id: 'm50', name: 'Vanguard Global Geoblocking (Modul 50)' },
      { id: 'm51', name: 'Vanguard Blacklist Routing (Modul 51)' },
      { id: 'm52', name: 'Labyrinth Tarpit Generator (Modul 52)' },
      { id: 'm53', name: 'Labyrinth Trap Engager (Modul 53)' },
      { id: 'm54', name: 'Labyrinth Endless Routing (Modul 54)' },
      { id: 'm55', name: 'Veritas Bio-Acoustic Scanner (Modul 55)' },
      { id: 'm56', name: 'Veritas Micro-Tremor AI (Modul 56)' },
      { id: 'm57', name: 'Veritas Matrix Lie Detector (Modul 57)' },
      { id: 'm58', name: 'Tartarus Honeypot Engine (Modul 58)' },
      { id: 'm59', name: 'Panopticon eBPF Supervisor (Modul 59)' },
      { id: 'm60', name: 'Panopticon Supreme-Eye Telemetry (Modul 60)' },
      { id: 'm61', name: 'Valkyrie Memory Allocator (Modul 61)' },
      { id: 'm62', name: 'Valkyrie Network Isolate (Modul 62)' },
      { id: 'm63', name: 'Valkyrie Threat Containment (Modul 63)' },
      { id: 'm64', name: 'Valkyrie Pre-Cognitive Brain (Modul 64)' },
      { id: 'm65', name: 'Valkyrie Matrix Sandbox (Modul 65)' },
      { id: 'm66', name: 'Valkyrie Eradication Engine (Modul 66)' },
      { id: 'm67', name: 'Cyber Warfare Command Hub (Modul 67)' }
    ]
  },
  group5: {
    name: 'KELOMPOK V: THE CULMINATION (Modul 68-75)',
    modules: [
      { id: 'm68', name: 'Scratch Container Musl Native (Modul 68)' },
      { id: 'm69', name: 'Scratch Container Tokio Alpine (Modul 69)' },
      { id: 'm70', name: 'The Aeon Shield Caddy Proxy (Modul 70)' },
      { id: 'm71', name: 'WASM SDK Forge Export (Modul 71)' },
      { id: 'm72', name: 'THE OMNI-GATEWAY Singularitas (Modul 72)' },
      { id: 'm73', name: 'The Aegis IAM Ledger (Modul 73)' },
      { id: 'm74', name: 'Identity Sync Orchestrator (Modul 74)' },
      { id: 'm75', name: 'The Neural Relay Lintas-VPS (Modul 75)' }
    ]
  },
  group6: {
    name: 'KELOMPOK VI: INFRASTRUCTURE SOVEREIGNTY (Modul 76-83)',
    modules: [
      { id: 'm76', name: 'Sovereign DNS Matrix (Modul 76)' },
      { id: 'm77', name: 'Global CDN Orchestrator (Modul 77)' },
      { id: 'm78', name: 'Edge Computing Node Alpha (Modul 78)' },
      { id: 'm79', name: 'Edge Computing Node Beta (Modul 79)' },
      { id: 'm80', name: 'Kubernetes Zero-Ops (Modul 80)' },
      { id: 'm81', name: 'Terraform State Sovereign (Modul 81)' },
      { id: 'm82', name: 'Ansible Playbook Forge (Modul 82)' },
      { id: 'm83', name: 'GitOps Pipeline Engine (Modul 83)' }
    ]
  },
  group7: {
    name: 'KELOMPOK VII: OBSERVABILITY MATRIX (Modul 84-90)',
    modules: [
      { id: 'm84', name: 'Observability Prometheus Matrix (Modul 84)' },
      { id: 'm85', name: 'Grafana Neural Dashboard (Modul 85)' },
      { id: 'm86', name: 'Jaeger Trace Interceptor (Modul 86)' },
      { id: 'm87', name: 'Loki Log Aggregator (Modul 87)' },
      { id: 'm88', name: 'Alert Manager Sentinel (Modul 88)' },
      { id: 'm89', name: 'Capacity Prediction AI (Modul 89)' },
      { id: 'm90', name: 'Cost Optimizer Neural (Modul 90)' }
    ]
  },
  group8: {
    name: 'KELOMPOK VIII: COMPLIANCE & RESILIENCE (Modul 91-99)',
    modules: [
      { id: 'm91', name: 'Compliance Regulator Matrix (Modul 91)' },
      { id: 'm92', name: 'GDPR Data Sovereignty Engine (Modul 92)' },
      { id: 'm93', name: 'ISO 27001 Auto-Audit (Modul 93)' },
      { id: 'm94', name: 'PCI-DSS Payment Shield (Modul 94)' },
      { id: 'm95', name: 'Penetration Test Orchestrator (Modul 95)' },
      { id: 'm96', name: 'Disaster Recovery Failover (Modul 96)' },
      { id: 'm97', name: 'Backup Sovereign Vault (Modul 97)' },
      { id: 'm98', name: 'Multi-Region Replication (Modul 98)' },
      { id: 'm99', name: 'The Omega Singularity (Modul 99)' }
    ]
  }
};

// Legacy compat: static export for immediate sync imports
export const MODULE_REGISTRY = STATIC_MODULE_REGISTRY;
