export default defineEventHandler(async (event) => {
  try {
    const { fileName, bucket } = getQuery(event);
    
    if (!fileName || !bucket) {
      return {
        success: false,
        error: 'Missing fileName or bucket parameter'
      };
    }
    
    console.log('[test-delete] Attempting to delete:', {
      fileName: fileName as string,
      bucket: bucket as string
    });
    
    // Import MinIO client
    const { getMinioClient } = await import('~/server/utils/minio');
    const client = getMinioClient();
    
    // Check if file exists first
    try {
      const stat = await client.statObject(bucket as string, fileName as string);
      console.log('[test-delete] File exists:', stat);
    } catch (error: any) {
      console.error('[test-delete] File not found:', error.message);
      return {
        success: false,
        error: 'File not found in bucket',
        details: error.message
      };
    }
    
    // Try to delete the file
    try {
      await client.removeObject(bucket as string, fileName as string);
      console.log('[test-delete] File deleted successfully');
      
      return {
        success: true,
        message: 'File deleted successfully'
      };
    } catch (error: any) {
      console.error('[test-delete] Delete failed:', error);
      return {
        success: false,
        error: 'Failed to delete file',
        details: error.message
      };
    }
  } catch (error: any) {
    console.error('[test-delete] Unexpected error:', error);
    return {
      success: false,
      error: 'Unexpected error',
      details: error.message
    };
  }
});
