import { createClient } from '@/lib/supabase/server' import { PrismaClient } from '@prisma/client' import { NextRequest, NextResponse } from 'next/server' const prisma = new PrismaClient() // GET /api/creator/dashboard - Get creator dashboard data export async function GET(req: NextRequest) { const supabase = await createClient() const { data: { user: supabaseUser }, error: authError } = await supabase.auth.getUser() if (authError || !supabaseUser) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } try { const user = await prisma.user.findUnique({ where: { supabaseId: supabaseUser.id }, include: { channels: { include: { stats: true, streams: { where: { isArchived: true }, orderBy: { endedAt: 'desc' }, take: 5, }, _count: { select: { followers: true, subscriptions: true, donations: true, }, }, }, }, }, }) if (!user || !user.isCreator) { return NextResponse.json( { error: 'User is not a creator' }, { status: 403 } ) } // Format response const channels = user.channels.map((channel) => ({ id: channel.id, name: channel.name, slug: channel.slug, followers: channel._count.followers, subscribers: channel._count.subscriptions, totalDonations: channel.stats?.totalDonations || 0, totalViews: channel.totalViews, stats: channel.stats, recentStreams: channel.streams, })) return NextResponse.json( { user: { id: user.id, displayName: user.displayName, email: user.email, avatarUrl: user.avatarUrl, }, channels, }, { status: 200 } ) } catch (error) { console.error('Error fetching dashboard:', error) return NextResponse.json( { error: 'Failed to fetch dashboard' }, { status: 500 } ) } }