export const dynamic = 'force-dynamic';

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

export async function GET() {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
    }
    const userId = (session.user as { id?: string })?.id;
    if (!userId) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
    }

    const [materials, tools, machines] = await Promise.all([
      prisma.userMaterial.findMany({ where: { userId }, orderBy: { createdAt: 'desc' } }),
      prisma.userTool.findMany({ where: { userId }, orderBy: { createdAt: 'desc' } }),
      prisma.userMachine.findMany({ where: { userId }, orderBy: { createdAt: 'desc' } }),
    ]);

    return NextResponse.json({ materials, tools, machines });
  } catch (error) {
    console.error('Error fetching user custom data:', error);
    return NextResponse.json({ error: 'Failed to fetch data' }, { status: 500 });
  }
}
