import { getBerths } from "../utils/nocodb";
import { requireAuth } from "../utils/auth";

export default defineEventHandler(async (event) => {
  console.log('[get-berths] Request received');

  // Check authentication (x-tag header OR Keycloak session)
  await requireAuth(event);

  try {
    console.log('[get-berths] Fetching berths...');
    const berths = await getBerths();
    console.log('[get-berths] Successfully fetched berths, count:', berths.list?.length || 0);
    return berths;
  } catch (error) {
    console.error('[get-berths] Error occurred:', error);
    console.error('[get-berths] Error details:', error instanceof Error ? error.message : 'Unknown error');
    throw error;
  }
});
