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

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

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

  try {
    const query = getQuery(event);
    const berthId = query.id;
    
    if (!berthId) {
      throw createError({ 
        statusCode: 400, 
        statusMessage: "Berth ID is required" 
      });
    }

    console.log('[get-berth-by-id] Fetching berth with ID:', berthId);
    const berth = await getBerthById(berthId as string);
    console.log('[get-berth-by-id] Successfully fetched berth:', berthId);
    
    return berth;
  } catch (error) {
    console.error('[get-berth-by-id] Error occurred:', error);
    console.error('[get-berth-by-id] Error details:', error instanceof Error ? error.message : 'Unknown error');
    throw error;
  }
});
