import React, { useState, useEffect, useRef } from 'react'; import { DataService, setApiMode } from '../services/dataService'; import { UserDTO, SystemConfig } from '../types'; import { ShieldAlert, Database, Users, Terminal, LogOut, Save, RefreshCw, Power, Lock, Search } from 'lucide-react'; import Image from 'next/image'; import Head from 'next/head'; export default function AdminConsole() { const [isAuthenticated, setIsAuthenticated] = useState(false); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); // Dashboard State const [activeTab, setActiveTab] = useState<'USERS' | 'DB' | 'LOGS'>('USERS'); const [users, setUsers] = useState([]); const [config, setConfig] = useState(null); const [logs, setLogs] = useState([]); const [currentMode, setCurrentMode] = useState('MOCK'); // Fake Terminal Logs const logContainerRef = useRef(null); useEffect(() => { // Client-side only init setCurrentMode(DataService.getMode()); }, []); useEffect(() => { if (isAuthenticated) { loadDashboardData(); const cleanup = startLogStream(); return cleanup; } }, [isAuthenticated]); useEffect(() => { if (logContainerRef.current) { logContainerRef.current.scrollTop = logContainerRef.current.scrollHeight; } }, [logs]); const startLogStream = () => { if (DataService.getMode() === 'REAL') { setLogs(['[SYSTEM] Attempting to connect to live backend log stream...', '[WARN] Log streaming over WebSocket not fully implemented in Preview']); return () => { }; } const phrases = [ "Connection pool optimized...", "User u_002 requested packet 0x4F...", "Analysing traffic pattern...", "Database heartbeat: STABLE", "WARNING: High latency on node US-EAST-4", "Backing up sector 7...", "New material indexed successfully." ]; const interval = setInterval(() => { const phrase = phrases[Math.floor(Math.random() * phrases.length)]; const time = new Date().toISOString().split('T')[1].replace('Z', ''); setLogs(prev => [...prev.slice(-20), `[${time}] ${phrase}`]); }, 2000); return () => clearInterval(interval); }; const loadDashboardData = async () => { try { const u = await DataService.getAllUsers(); setUsers(u); const c = await DataService.getSystemConfig(); setConfig(c); } catch (e) { setLogs(prev => [...prev, `[ERROR] Failed to fetch data: ${e}`]); } }; const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setError(''); const success = await DataService.verifyAdmin(username, password); if (success) { setIsAuthenticated(true); setLogs(['[SYSTEM] Root access granted.', '[SYSTEM] Initializing admin daemon...']); } else { setError('ACCESS_DENIED // INVALID_CREDENTIALS'); } setLoading(false); }; const handleConfigSave = async () => { if (!config) return; setLoading(true); try { await DataService.updateSystemConfig(config); setLogs(prev => [...prev, '[SYSTEM] Database configuration updated. Restarting services...']); alert("SYSTEM CONFIG UPDATED."); } catch (e) { alert("Failed to update config."); } setLoading(false); }; const handleToggleUser = async (uid: string) => { const updated = await DataService.toggleUserStatus(uid); if (updated) { setUsers(users.map(u => u.id === uid ? updated : u)); setLogs(prev => [...prev, `[ADMIN] User ${updated.username} status changed to ${updated.status}`]); } }; const handleUpdateRole = async (uid: string, role: 'USER' | 'CREATOR' | 'MANAGER' | 'ADMIN') => { try { const updated = await DataService.updateUserRole(uid, role as any); setUsers(prev => prev.map(u => u.id === uid ? updated : u)); setLogs(prev => [...prev, `[ADMIN] Role of ${updated.username} -> ${updated.role}`]); } catch (e) { setLogs(prev => [...prev, `[ERROR] Failed to update role: ${e}`]); } }; const handleModeSwitch = (mode: 'MOCK' | 'REAL') => { if (mode === 'REAL' && !confirm("WARNING: Switching to REAL mode will attempt to connect to a running MySQL backend at localhost:3000. \n\nContinue?")) { return; } setApiMode(mode); setCurrentMode(mode); }; if (!isAuthenticated) { return (
NEXUS_MAT.OS {/* Background Grid */}

NEXUS_CORE

RESTRICTED AREA // AUTHORIZED PERSONNEL ONLY

setUsername(e.target.value)} className="w-full bg-red-950/20 border border-red-900 p-3 text-red-500 focus:outline-none focus:border-red-500 transition-colors" placeholder="admin" />
setPassword(e.target.value)} className="w-full bg-red-950/20 border border-red-900 p-3 text-red-500 focus:outline-none focus:border-red-500 transition-colors" placeholder="••••••••" />
{error && (
{error}
)}
SYSTEM_ID: 0x8842-ALPHA
IP_LOGGED: 127.0.0.1
); } return (
NEXUS_MAT.OS {/* Top Bar */}
CORE.CONSOLE ADMIN_MODE
{currentMode === 'REAL' ? 'LIVE_DATABASE' : 'SIMULATION_MODE'}
{/* Sidebar */} {/* Main Content */}
{/* Background Decoration */}
{activeTab === 'USERS' && (

USER_DATABASE

{users.map(user => ( ))}
AVATAR IDENTIFIER ROLE STATUS CREATED ACTIONS
avatar {user.username}
{user.id}
{user.status} {new Date(user.createdAt).toLocaleDateString()}
)} {activeTab === 'DB' && config && (

MYSQL_CONFIGURATION
DATA_SOURCE_MODE:

setConfig({ ...config, dbHost: e.target.value })} className="w-full bg-black border border-red-900 p-3 text-white focus:border-red-500 outline-none" />
setConfig({ ...config, dbPort: e.target.value })} className="w-full bg-black border border-red-900 p-3 text-white focus:border-red-500 outline-none" />
setConfig({ ...config, dbUser: e.target.value })} className="w-full bg-black border border-red-900 p-3 text-white focus:border-red-500 outline-none" />
setConfig({ ...config, dbPass: e.target.value })} className="w-full bg-black border border-red-900 p-3 text-white focus:border-red-500 outline-none" />
setConfig({ ...config, dbName: e.target.value })} className="w-full bg-black border border-red-900 p-3 text-white focus:border-red-500 outline-none" />
setConfig({ ...config, maxUploadMB: Number(e.target.value) || 0 })} className="w-full bg-black border border-red-900 p-3 text-white focus:border-red-500 outline-none" />
{currentMode === 'MOCK' && (
[INFO] Currently running in SIMULATION MODE.
Configuration editing is disabled.
Switch to LIVE_MYSQL to configure connection string.
)}
)} {activeTab === 'LOGS' && (

KERNEL_LOGS

{logs.map((log, i) => (
{i.toString().padStart(4, '0')} {log}
))}
)}
); }