export default defineEventHandler(async (event) => {
  try {
    const { bucket, prefix } = getQuery(event);
    
    if (!bucket) {
      return {
        success: false,
        error: 'Missing bucket parameter'
      };
    }
    
    console.log('[test-list-bucket] Listing files:', {
      bucket: bucket as string,
      prefix: prefix as string || ''
    });
    
    // Import MinIO client
    const { getMinioClient } = await import('~/server/utils/minio');
    const client = getMinioClient();
    
    const files: any[] = [];
    
    await new Promise((resolve, reject) => {
      const stream = client.listObjectsV2(bucket as string, prefix as string || '', true);
      
      stream.on('data', (obj) => {
        if (obj && obj.name) {
          files.push({
            name: obj.name,
            size: obj.size,
            lastModified: obj.lastModified
          });
        }
      });
      
      stream.on('error', reject);
      stream.on('end', resolve);
    });
    
    return {
      success: true,
      bucket: bucket as string,
      prefix: prefix as string || '',
      count: files.length,
      files: files
    };
  } catch (error: any) {
    console.error('[test-list-bucket] Error:', error);
    return {
      success: false,
      error: 'Failed to list bucket',
      details: error.message
    };
  }
});
