'use client' import { useState, useEffect } from 'react' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Label } from '@/components/ui/label' import { Input } from '@/components/ui/input' import { Textarea } from '@/components/ui/textarea' import { Switch } from '@/components/ui/switch' import { Badge } from '@/components/ui/badge' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, } from '@/components/ui/dialog' import { Settings, Save, RefreshCw, Database, Bell, Shield, Users, Activity } from 'lucide-react' import { toast } from '@/hooks/use-toast' import { formatDistanceToNow } from 'date-fns' interface SystemSettingsData { maintenanceMode: boolean registrationEnabled: boolean emailVerificationRequired: boolean maxUserBalance: number defaultUserRole: 'user' | 'admin' systemMessage: string paymentGatewayEnabled: boolean developerHireEnabled: boolean vpsDeploymentEnabled: boolean kubernetesDeploymentEnabled: boolean vpnServiceEnabled: boolean lastUpdated: string updatedBy: string } interface SystemStatistics { totalUsers: number adminUsers: number verifiedUsers: number unverifiedUsers: number } interface RecentActivity { id: string action: string details: string timestamp: string adminName: string } export default function SystemSettings() { const [settings, setSettings] = useState(null) const [statistics, setStatistics] = useState(null) const [recentActivities, setRecentActivities] = useState([]) const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) const [notificationDialog, setNotificationDialog] = useState(false) const [notificationForm, setNotificationForm] = useState({ message: '', targetUsers: 'all', }) useEffect(() => { fetchSettings() }, []) const fetchSettings = async () => { try { const token = localStorage.getItem('accessToken') || document.cookie .split('; ') .find((row) => row.startsWith('accessToken=')) ?.split('=')[1] const response = await fetch('/api/admin/settings', { headers: { Authorization: `Bearer ${token}`, }, }) if (!response.ok) { throw new Error('Failed to fetch settings') } const data = await response.json() setSettings(data.settings) setStatistics(data.statistics) setRecentActivities(data.recentActivities) } catch (error) { console.error('Settings fetch error:', error) toast({ title: 'Error', description: 'Failed to load system settings', variant: 'destructive', }) } finally { setLoading(false) } } const handleSaveSettings = async () => { if (!settings) return setSaving(true) try { const token = localStorage.getItem('accessToken') || document.cookie .split('; ') .find((row) => row.startsWith('accessToken=')) ?.split('=')[1] const response = await fetch('/api/admin/settings', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}`, }, body: JSON.stringify({ action: 'updateSettings', settings, }), }) if (!response.ok) { throw new Error('Failed to update settings') } const data = await response.json() setSettings(data.settings) toast({ title: 'Success', description: data.message, }) } catch (error) { console.error('Settings update error:', error) toast({ title: 'Error', description: 'Failed to update settings', variant: 'destructive', }) } finally { setSaving(false) } } const handleSystemAction = async (action: string, additionalData?: any) => { try { const token = localStorage.getItem('accessToken') || document.cookie .split('; ') .find((row) => row.startsWith('accessToken=')) ?.split('=')[1] const response = await fetch('/api/admin/settings', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}`, }, body: JSON.stringify({ action, ...additionalData, }), }) if (!response.ok) { throw new Error(`Failed to ${action}`) } const data = await response.json() toast({ title: 'Success', description: data.message, }) if (action === 'sendSystemNotification') { setNotificationDialog(false) setNotificationForm({ message: '', targetUsers: 'all' }) } } catch (error) { console.error(`${action} error:`, error) toast({ title: 'Error', description: `Failed to ${action}`, variant: 'destructive', }) } } const handleSendNotification = () => { handleSystemAction('sendSystemNotification', notificationForm) } if (loading) { return (
{Array.from({ length: 4 }).map((_, i) => (
))}
) } if (!settings || !statistics) { return (

Failed to load system settings

) } return (
{/* System Statistics */}

Total Users

{statistics.totalUsers}

Admin Users

{statistics.adminUsers}

Verified Users

{statistics.verifiedUsers}

Unverified Users

{statistics.unverifiedUsers}

{/* System Settings */} System Settings {/* General Settings */}

General

Disable access for non-admin users

setSettings({ ...settings, maintenanceMode: checked }) } />

Allow new user registrations

setSettings({ ...settings, registrationEnabled: checked }) } />

Require email verification for new users

setSettings({ ...settings, emailVerificationRequired: checked }) } />
setSettings({ ...settings, maxUserBalance: parseInt(e.target.value) || 0 }) } />