import { NextResponse } from 'next/server';
import { query } from '@/lib/db';
import { getAdminSession } from '@/lib/auth';

export async function GET() {
  const session = await getAdminSession();
  if (!session) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
  }

  try {
    const supplements = await query(
      `SELECT id, name, slug, brand, image, benefits, price FROM Product WHERE categoryId = 'cat_supps'`
    ) as any[];
    return NextResponse.json({ supplements });
  } catch (err: any) {
    console.error('[admin/supplements] GET error:', err?.message ?? err);
    return NextResponse.json({ error: 'Failed to load supplements.' }, { status: 500 });
  }
}
