import { NextRequest, NextResponse } from 'next/server';
import { PrismaClient } from '@prisma/client';
import { requireStaffAuth } from '@/lib/auth';

const prisma = new PrismaClient();

export async function GET(request: NextRequest) {
    const { error, context } = await requireStaffAuth(request);
    if (error || !context) return error!;

    if (context.role !== 'curator') {
        return NextResponse.json(
            { error: 'Only curators can access this endpoint' },
            { status: 403 }
        );
    }

    // Specials, die diesem Kurator zugewiesen sind
    const assignments = await prisma.curatorSpecial.findMany({
        where: { curatorId: context.curator.id },
        select: { specialId: true },
    });

    if (assignments.length === 0) {
        return NextResponse.json([]);
    }

    const specialIds = assignments.map(a => a.specialId);

    const specials = await prisma.special.findMany({
        where: { id: { in: specialIds } },
        include: {
            songs: true,
        },
        orderBy: { id: 'asc' },
    });

    const result = specials.map(special => ({
        id: special.id,
        name: special.name,
        songCount: special.songs.length,
    }));

    return NextResponse.json(result);
}


