import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { prisma } from '@/lib/prisma';

// Returns the distinct set of tabs (and list titles within each tab) for the
// current user's someday lists — used by the print/export modal to show a
// tab-filter selector.
export async function GET() {
    const session = await getServerSession(authOptions);
    if (!session?.user?.email) return new NextResponse('Unauthorized', { status: 401 });

    const user = await prisma.user.findUnique({
        where: { email: session.user.email },
        select: { id: true },
    });
    if (!user) return new NextResponse('Not found', { status: 404 });

    const lists = await prisma.somedayList.findMany({
        where: { userId: user.id },
        select: { id: true, title: true, tab: true },
        orderBy: [{ tab: 'asc' }, { order: 'asc' }],
    });

    return NextResponse.json({ lists });
}
