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

export async function GET() {
  try {
    const session = await getServerSession(authOptions);

    if (!session || session.user.role !== 'ADMIN') {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
    }

    const equipment = await prisma.equipment.findMany({
      where: { status: 'AVAILABLE' },
      orderBy: { name: 'asc' },
      select: {
        id: true,
        name: true,
        type: true,
        price: true,
      },
    });

    return NextResponse.json({ equipment });
  } catch (error) {
    console.error('Equipment GET error:', error);
    return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
  }
}
