export default defineEventHandler(async (event) => {
  const xTagHeader = getRequestHeader(event, "x-tag");
  console.log('[get-berths] Request received with x-tag:', xTagHeader);

  if (!xTagHeader || (xTagHeader !== "094ut234" && xTagHeader !== "pjnvü1230")) {
    console.error('[get-berths] Authentication failed - invalid x-tag:', xTagHeader);
    throw createError({ statusCode: 401, statusMessage: "unauthenticated" });
  }

  try {
    const config = getNocoDbConfiguration();
    const berthsTableId = "mczgos9hr3oa9qc";
    
    console.log('[get-berths] Fetching berths...');
    const berths = await $fetch<{ list: any[] }>(`${config.url}/api/v2/tables/${berthsTableId}/records`, {
      headers: {
        "xc-token": config.token,
      },
      params: {
        limit: 1000,
      },
    });
    
    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;
  }
});
