import { NextRequest, NextResponse } from 'next/server' import { withAdminAuth } from '@/lib/admin-middleware' import { connectDB } from '@/lib/mongodb' import { User } from '@/models/user' import { z } from 'zod' import { getSystemSettings, updateSystemSettings } from '@/lib/system-settings' const SystemSettingsSchema = z.object({ maintenanceMode: z.boolean().optional(), registrationEnabled: z.boolean().optional(), emailVerificationRequired: z.boolean().optional(), maxUserBalance: z.number().min(0).optional(), defaultUserRole: z.enum(['user', 'admin']).optional(), systemMessage: z.string().optional(), paymentGatewayEnabled: z.boolean().optional(), developerHireEnabled: z.boolean().optional(), vpsDeploymentEnabled: z.boolean().optional(), kubernetesDeploymentEnabled: z.boolean().optional(), vpnServiceEnabled: z.boolean().optional(), }) // System settings are now managed by the system-settings service export async function GET(request: NextRequest) { return withAdminAuth(request, async (req, admin) => { try { await connectDB() // Get system statistics const totalUsers = await User.countDocuments() const adminUsers = await User.countDocuments({ role: 'admin' }) const verifiedUsers = await User.countDocuments({ isVerified: true }) const unverifiedUsers = await User.countDocuments({ isVerified: false }) // Get recent admin activities (mock data for now) const recentActivities = [ { id: '1', action: 'User role updated', details: 'Changed user role from user to admin', timestamp: new Date().toISOString(), adminName: admin.name, }, ] return NextResponse.json({ settings: await getSystemSettings(), statistics: { totalUsers, adminUsers, verifiedUsers, unverifiedUsers, }, recentActivities, }) } catch (error) { console.error('Admin settings fetch error:', error) return NextResponse.json({ error: 'Failed to fetch system settings' }, { status: 500 }) } }) } export async function POST(request: NextRequest) { return withAdminAuth(request, async (req, admin) => { try { const body = await request.json() const { action, settings } = body if (action === 'updateSettings') { const validatedSettings = SystemSettingsSchema.parse(settings) // Update system settings const updatedSettings = { ...validatedSettings, lastUpdated: new Date().toISOString(), updatedBy: admin.name, } await updateSystemSettings(updatedSettings) return NextResponse.json({ settings: await getSystemSettings(), message: 'Settings updated successfully', }) } if (action === 'clearCache') { // Mock cache clearing - in a real app, this would clear Redis/memory cache return NextResponse.json({ message: 'System cache cleared successfully', }) } if (action === 'backupDatabase') { // Mock database backup - in a real app, this would trigger a backup process return NextResponse.json({ message: 'Database backup initiated successfully', backupId: `backup_${Date.now()}`, }) } if (action === 'sendSystemNotification') { const { message, targetUsers } = body // Mock system notification - in a real app, this would send notifications return NextResponse.json({ message: `System notification sent to ${targetUsers} users`, notificationId: `notif_${Date.now()}`, }) } return NextResponse.json({ error: 'Invalid action' }, { status: 400 }) } catch (error) { console.error('Admin settings update error:', error) return NextResponse.json({ error: 'Failed to update system settings' }, { status: 500 }) } }) }