import { NextResponse } from 'next/server';
import { db, writerDb } from "@/drizzle/db";
import { systemFeatures } from "@/drizzle/schema";
import { eq } from 'drizzle-orm';

export async function GET() {
  try {
    const modules = [
      // VIDEO ENGINE (XCU) - 25 Modules
      { name: "XCU AV1 Codec (4K)", key: "xcu.codec.av1", module: "XCU", defaultState: "UPSELL" },
      { name: "XCU HEVC Codec (2K)", key: "xcu.codec.hevc", module: "XCU", defaultState: "UPSELL" },
      { name: "XCU VP9 Optimized", key: "xcu.codec.vp9", module: "XCU", defaultState: "GRANTED" },
      { name: "WebTransport / QUIC", key: "xcu.transport.quic", module: "XCU", defaultState: "GRANTED" },
      { name: "MoQ (Media over QUIC)", key: "xcu.transport.moq", module: "XCU", defaultState: "UPSELL" },
      { name: "Auto-Pilot Codec Routing", key: "xcu.feature.autopilot", module: "XCU", defaultState: "UPSELL" },
      { name: "Ultra-Low Latency (0ms)", key: "xcu.feature.ull", module: "XCU", defaultState: "GRANTED" },
      { name: "Global Mesh Relay", key: "xcu.feature.mesh", module: "XCU", defaultState: "GRANTED" },
      { name: "Simulcast Matrix", key: "xcu.feature.simulcast", module: "XCU", defaultState: "GRANTED" },
      { name: "SVC (Scalable Video Coding)", key: "xcu.feature.svc", module: "XCU", defaultState: "UPSELL" },
      { name: "XCU On-Premise Gateway", key: "xcu.feature.onprem", module: "XCU", defaultState: "HIDDEN" },
      { name: "Dynamic Bitrate Adaptation", key: "xcu.feature.dba", module: "XCU", defaultState: "GRANTED" },
      { name: "Hardware Acceleration", key: "xcu.feature.hwaccel", module: "XCU", defaultState: "GRANTED" },
      { name: "Screen Share 60FPS", key: "xcu.feature.screenshare_high", module: "XCU", defaultState: "UPSELL" },
      { name: "Virtual Background AI", key: "xcu.feature.vbg_ai", module: "XCU", defaultState: "UPSELL" },
      { name: "Noise Suppression AI", key: "xcu.feature.noise_ai", module: "XCU", defaultState: "GRANTED" },
      { name: "Echo Cancellation Ultra", key: "xcu.feature.aec_ultra", module: "XCU", defaultState: "GRANTED" },
      { name: "Multi-Camera Broadcast", key: "xcu.feature.multicam", module: "XCU", defaultState: "UPSELL" },
      { name: "Live Canvas TTE", key: "xcu.feature.tte", module: "XCU", defaultState: "UPSELL" },
      { name: "Breakout Matrix Rooms", key: "xcu.feature.breakout", module: "XCU", defaultState: "UPSELL" },
      { name: "Recording Sovereign", key: "xcu.feature.recording", module: "XCU", defaultState: "UPSELL" },
      { name: "Live Streaming RTMP/HLS", key: "xcu.feature.stream_out", module: "XCU", defaultState: "UPSELL" },
      { name: "Watermark Dynamic", key: "xcu.feature.watermark", module: "XCU", defaultState: "GRANTED" },
      { name: "Telepathy Sync State", key: "xcu.feature.telepathy", module: "XCU", defaultState: "GRANTED" },
      { name: "XCU Kernel Bypass (eBPF)", key: "xcu.feature.ebpf", module: "XCU", defaultState: "HIDDEN" },

      // CHAT ENGINE (XTM) - 25 Modules
      { name: "End-to-End Encryption", key: "xtm.security.e2ee", module: "XTM", defaultState: "GRANTED" },
      { name: "Omni-Brain Interceptor", key: "xtm.ai.interceptor", module: "XTM", defaultState: "UPSELL" },
      { name: "The Vault (DRM Storage)", key: "xtm.storage.vault", module: "XTM", defaultState: "UPSELL" },
      { name: "Self-Destruct Messages", key: "xtm.feature.burn", module: "XTM", defaultState: "UPSELL" },
      { name: "Quantum Handshake", key: "xtm.security.quantum", module: "XTM", defaultState: "HIDDEN" },
      { name: "Biometric Auth Chat", key: "xtm.security.biometric", module: "XTM", defaultState: "UPSELL" },
      { name: "Persistent Message Log", key: "xtm.feature.persistence", module: "XTM", defaultState: "GRANTED" },
      { name: "Large File Transfer (10GB)", key: "xtm.feature.file_large", module: "XTM", defaultState: "UPSELL" },
      { name: "Global Search Index", key: "xtm.feature.search", module: "XTM", defaultState: "GRANTED" },
      { name: "Group Management Pro", key: "xtm.feature.group_pro", module: "XTM", defaultState: "GRANTED" },
      { name: "Audit Trail Chat", key: "xtm.feature.audit", module: "XTM", defaultState: "UPSELL" },
      { name: "Bot Integration API", key: "xtm.feature.bot", module: "XTM", defaultState: "UPSELL" },
      { name: "Reaction Matrix", key: "xtm.feature.reaction", module: "XTM", defaultState: "GRANTED" },
      { name: "Threaded Conversations", key: "xtm.feature.threads", module: "XTM", defaultState: "GRANTED" },
      { name: "Translate Auto AI", key: "xtm.ai.translate", module: "XTM", defaultState: "UPSELL" },
      { name: "Sentiment Analysis", key: "xtm.ai.sentiment", module: "XTM", defaultState: "UPSELL" },
      { name: "Summary Neural Link", key: "xtm.ai.summary", module: "XTM", defaultState: "UPSELL" },
      { name: "Offline Sync Mode", key: "xtm.feature.offline", module: "XTM", defaultState: "GRANTED" },
      { name: "Multi-Device Sync", key: "xtm.feature.multidevice", module: "XTM", defaultState: "GRANTED" },
      { name: "Whisper Mode", key: "xtm.feature.whisper", module: "XTM", defaultState: "UPSELL" },
      { name: "Announcement Channel", key: "xtm.feature.broadcast", module: "XTM", defaultState: "GRANTED" },
      { name: "Read Receipts Stealth", key: "xtm.feature.stealth", module: "XTM", defaultState: "UPSELL" },
      { name: "Custom Emoji Pack", key: "xtm.feature.emoji", module: "XTM", defaultState: "GRANTED" },
      { name: "Voice Note Matrix", key: "xtm.feature.voicenote", module: "XTM", defaultState: "GRANTED" },
      { name: "Video Message Circle", key: "xtm.feature.videonote", module: "XTM", defaultState: "UPSELL" },

      // IAM / CORE - 20 Modules
      { name: "Supreme Eye Dashboard", key: "iam.feature.supreme_eye", module: "IAM", defaultState: "HIDDEN" },
      { name: "BYOK Matrix Control", key: "iam.feature.byok", module: "IAM", defaultState: "UPSELL" },
      { name: "White-Label Branding", key: "iam.feature.branding", module: "IAM", defaultState: "UPSELL" },
      { name: "Sovereign VAULT Auth", key: "iam.auth.vault", module: "IAM", defaultState: "UPSELL" },
      { name: "Zero-Trust SSO", key: "iam.auth.sso", module: "IAM", defaultState: "UPSELL" },
      { name: "Xendit Payment Bridge", key: "iam.billing.xendit", module: "IAM", defaultState: "GRANTED" },
      { name: "Multi-Currency Support", key: "iam.billing.currency", module: "IAM", defaultState: "GRANTED" },
      { name: "Audit Log Export", key: "iam.security.audit_log", module: "IAM", defaultState: "UPSELL" },
      { name: "IP Whitelisting", key: "iam.security.ip_whitelist", module: "IAM", defaultState: "UPSELL" },
      { name: "License Generator", key: "iam.admin.licenses", module: "IAM", defaultState: "HIDDEN" },
      { name: "Tenant Isolation Pro", key: "iam.security.isolation", module: "IAM", defaultState: "GRANTED" },
      { name: "Advanced Role Matrix", key: "iam.security.roles", module: "IAM", defaultState: "GRANTED" },
      { name: "Usage Telemetry", key: "iam.feature.telemetry", module: "IAM", defaultState: "GRANTED" },
      { name: "API Gateway Access", key: "iam.feature.api_access", module: "IAM", defaultState: "UPSELL" },
      { name: "Custom Domain Mapping", key: "iam.feature.custom_domain", module: "IAM", defaultState: "UPSELL" },
      { name: "B2B Marketplace", key: "iam.feature.marketplace", module: "IAM", defaultState: "UPSELL" },
      { name: "Developer Sandbox", key: "iam.feature.sandbox", module: "IAM", defaultState: "UPSELL" },
      { name: "System Mindmap", key: "iam.feature.mindmap", module: "IAM", defaultState: "HIDDEN" },
      { name: "Quantum Logs", key: "iam.feature.quantum_logs", module: "IAM", defaultState: "HIDDEN" },
      { name: "Emergency Kill-Switch", key: "iam.security.killswitch", module: "IAM", defaultState: "HIDDEN" },

      // JVC (JUMPA Video Conference) UI Controls - 14 Modules
      { name: "Tombol Microphone", key: "jvc.ui.microphone", module: "JVC", defaultState: "GRANTED" },
      { name: "Tombol Camera + Flip", key: "jvc.ui.camera", module: "JVC", defaultState: "GRANTED" },
      { name: "Panel Peserta", key: "jvc.ui.people_panel", module: "JVC", defaultState: "GRANTED" },
      { name: "Panel Chat", key: "jvc.ui.chat_panel", module: "JVC", defaultState: "GRANTED" },
      { name: "Screen Share", key: "jvc.ui.screenshare", module: "JVC", defaultState: "GRANTED" },
      { name: "Emoji Reactions", key: "jvc.ui.reactions", module: "JVC", defaultState: "GRANTED" },
      { name: "Breakout Rooms", key: "jvc.ui.breakout", module: "JVC", defaultState: "GRANTED" },
      { name: "Recording", key: "jvc.ui.recording", module: "JVC", defaultState: "UPSELL" },
      { name: "Neural Radiance Filter", key: "jvc.ui.beauty_filter", module: "JVC", defaultState: "GRANTED" },
      { name: "Virtual Background + Bokeh", key: "jvc.ui.virtual_bg", module: "JVC", defaultState: "GRANTED" },
      { name: "Meeting Timer", key: "jvc.ui.timer", module: "JVC", defaultState: "GRANTED" },
      { name: "XCO Command Matrix Panel", key: "jvc.ui.matrix_command", module: "JVC", defaultState: "GRANTED" },
      { name: "Gallery/Speaker View Toggle", key: "jvc.ui.layout_toggle", module: "JVC", defaultState: "GRANTED" },
      { name: "Host Controls (Mute All, Kick)", key: "jvc.ui.host_controls", module: "JVC", defaultState: "GRANTED" },
      { name: "Video Engine Selector (XCO)", key: "jvc.xco.video_engine", module: "JVC", defaultState: "GRANTED" },
      { name: "Audio Engine Selector (XCO)", key: "jvc.xco.audio_engine", module: "JVC", defaultState: "GRANTED" },
      { name: "FPS Selector (XCO)", key: "jvc.xco.fps_selector", module: "JVC", defaultState: "GRANTED" },

      // JC (JUMPA Chat) UI Controls - 12 Modules
      { name: "Kirim Pesan Text", key: "jc.ui.send_message", module: "JC", defaultState: "GRANTED" },
      { name: "Emoji Reactions Chat", key: "jc.ui.reactions", module: "JC", defaultState: "GRANTED" },
      { name: "File Upload", key: "jc.ui.file_upload", module: "JC", defaultState: "GRANTED" },
      { name: "Voice Notes", key: "jc.ui.voice_notes", module: "JC", defaultState: "GRANTED" },
      { name: "OmniBrain AI Chat", key: "jc.ui.omnibrain", module: "JC", defaultState: "UPSELL" },
      { name: "Edit Message", key: "jc.ui.edit_message", module: "JC", defaultState: "GRANTED" },
      { name: "Delete Message", key: "jc.ui.delete_message", module: "JC", defaultState: "GRANTED" },
      { name: "Group Management", key: "jc.ui.group_manage", module: "JC", defaultState: "GRANTED" },
      { name: "Typing Indicator", key: "jc.ui.typing_indicator", module: "JC", defaultState: "GRANTED" },
      { name: "Read Receipts", key: "jc.ui.read_receipts", module: "JC", defaultState: "GRANTED" },
      { name: "Video Call from Chat", key: "jc.ui.video_call", module: "JC", defaultState: "GRANTED" },
      { name: "Voice Call from Chat", key: "jc.ui.voice_call", module: "JC", defaultState: "GRANTED" },
    ];

    // Seed logic
    for (const m of modules) {
      const existing = await db.select().from(systemFeatures).where(eq(systemFeatures.key, m.key)).limit(1);
      if (existing.length === 0) {
        await writerDb.insert(systemFeatures).values(m);
      } else {
        // Update to ensure defaults are correct
        await writerDb.update(systemFeatures).set({ name: m.name, module: m.module }).where(eq(systemFeatures.key, m.key));
      }
    }

    return NextResponse.json({ 
      success: true, 
      message: `${modules.length} Quantum Modules Synchronized`,
      count: modules.length
    });

  } catch (e) {
    const message = e instanceof Error ? e.message : "Internal Error during Seeding";
    return NextResponse.json({ error: message }, { status: 500 });
  }
}
