import { NextRequest, NextResponse } from 'next/server' import { authMiddleware } from '@/lib/auth-middleware' import connectDB from '@/lib/mongodb' import { User as UserModel } from '@/models/user' import BillingService from '@/lib/billing-service' // GET endpoint to fetch user's billing statistics export async function GET(request: NextRequest) { try { const user = await authMiddleware(request) if (!user) { return NextResponse.json( { success: false, error: { message: 'Authentication required', code: 'UNAUTHORIZED' }, }, { status: 401 } ) } await connectDB() // Get user data const userData = await UserModel.findOne({ email: user.email }) if (!userData) { return NextResponse.json( { success: false, error: { message: 'User not found', code: 'USER_NOT_FOUND' }, }, { status: 404 } ) } // Get comprehensive billing statistics const stats = await BillingService.getBillingStats(user.email, user.id) const activeServices = await BillingService.getActiveServices(user.email, user.id) return NextResponse.json({ success: true, data: { ...stats, activeServicesList: activeServices, currentBalance: userData.balance || 0, }, }) } catch (error) { console.error('Failed to fetch billing statistics:', error) return NextResponse.json( { success: false, error: { message: 'Failed to fetch billing statistics', code: 'INTERNAL_ERROR' }, }, { status: 500 } ) } }