/**
 * Script to list all files in MinIO xpeditis-documents bucket
 */

const { S3Client, ListObjectsV2Command } = require('@aws-sdk/client-s3');
require('dotenv').config();

const MINIO_ENDPOINT = process.env.AWS_S3_ENDPOINT || 'http://localhost:9000';
const BUCKET_NAME = 'xpeditis-documents';

// Initialize MinIO client
const s3Client = new S3Client({
  region: 'us-east-1',
  endpoint: MINIO_ENDPOINT,
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID || 'minioadmin',
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || 'minioadmin',
  },
  forcePathStyle: true,
});

async function listFiles() {
  try {
    console.log(`📋 Listing all files in bucket: ${BUCKET_NAME}\n`);

    let allFiles = [];
    let continuationToken = null;

    do {
      const command = new ListObjectsV2Command({
        Bucket: BUCKET_NAME,
        ContinuationToken: continuationToken,
      });

      const response = await s3Client.send(command);

      if (response.Contents) {
        allFiles = allFiles.concat(response.Contents);
      }

      continuationToken = response.NextContinuationToken;
    } while (continuationToken);

    console.log(`Found ${allFiles.length} files total:\n`);

    // Group by booking ID
    const byBooking = {};
    allFiles.forEach(file => {
      const parts = file.Key.split('/');
      if (parts.length >= 3 && parts[0] === 'csv-bookings') {
        const bookingId = parts[1];
        if (!byBooking[bookingId]) {
          byBooking[bookingId] = [];
        }
        byBooking[bookingId].push({
          key: file.Key,
          size: file.Size,
          lastModified: file.LastModified,
        });
      } else {
        console.log(`  Other: ${file.Key} (${file.Size} bytes)`);
      }
    });

    console.log(`\nFiles grouped by booking:\n`);
    Object.entries(byBooking).forEach(([bookingId, files]) => {
      console.log(`📦 Booking: ${bookingId.substring(0, 8)}...`);
      files.forEach(file => {
        const filename = file.key.split('/').pop();
        console.log(`   - ${filename} (${file.size} bytes) - ${file.lastModified}`);
      });
      console.log('');
    });

    console.log(`\n📊 Summary:`);
    console.log(`   Total files: ${allFiles.length}`);
    console.log(`   Bookings with files: ${Object.keys(byBooking).length}`);
  } catch (error) {
    console.error('❌ Error:', error);
    throw error;
  }
}

listFiles()
  .then(() => {
    console.log('\n✅ Script completed successfully');
    process.exit(0);
  })
  .catch(error => {
    console.error('\n❌ Script failed:', error);
    process.exit(1);
  });
