import { deleteInterest } from '~/server/utils/nocodb';

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

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

  try {
    const body = await readBody(event);
    const { id } = body;
    console.log('[delete-interest] Request body:', { id });

    if (!id) {
      console.error('[delete-interest] Missing ID in request');
      throw createError({ statusCode: 400, statusMessage: "ID is required" });
    }

    console.log('[delete-interest] Deleting interest:', id);
    const result = await deleteInterest(id);
    console.log('[delete-interest] Successfully deleted interest:', id);
    
    return result;
  } catch (error) {
    console.error('[delete-interest] Error occurred:', error);
    console.error('[delete-interest] Error stack:', error instanceof Error ? error.stack : 'No stack trace');
    
    if (error instanceof Error) {
      throw createError({ statusCode: 500, statusMessage: error.message });
    } else {
      throw createError({
        statusCode: 500,
        statusMessage: "An unexpected error occurred",
      });
    }
  }
});
