/**
 * Script to delete test documents from MinIO
 *
 * Deletes only small test files (< 1000 bytes) created by upload-test-documents.js
 * Preserves real uploaded documents (larger files)
 */

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

const MINIO_ENDPOINT = process.env.AWS_S3_ENDPOINT || 'http://localhost:9000';
const BUCKET_NAME = 'xpeditis-documents';
const TEST_FILE_SIZE_THRESHOLD = 1000; // Files smaller than 1KB are likely test files

// 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 deleteTestDocuments() {
  try {
    console.log('📋 Listing all files in bucket:', BUCKET_NAME);

    // List all files
    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(`\n📊 Found ${allFiles.length} total files\n`);

    // Filter test files (small files < 1000 bytes)
    const testFiles = allFiles.filter(file => file.Size < TEST_FILE_SIZE_THRESHOLD);
    const realFiles = allFiles.filter(file => file.Size >= TEST_FILE_SIZE_THRESHOLD);

    console.log(`🔍 Analysis:`);
    console.log(`   Test files (< ${TEST_FILE_SIZE_THRESHOLD} bytes): ${testFiles.length}`);
    console.log(`   Real files (>= ${TEST_FILE_SIZE_THRESHOLD} bytes): ${realFiles.length}\n`);

    if (testFiles.length === 0) {
      console.log('✅ No test files to delete');
      return;
    }

    console.log(`🗑️  Deleting ${testFiles.length} test files:\n`);

    let deletedCount = 0;
    for (const file of testFiles) {
      console.log(`   Deleting: ${file.Key} (${file.Size} bytes)`);

      try {
        await s3Client.send(
          new DeleteObjectCommand({
            Bucket: BUCKET_NAME,
            Key: file.Key,
          })
        );
        deletedCount++;
      } catch (error) {
        console.error(`   ❌ Failed to delete ${file.Key}:`, error.message);
      }
    }

    console.log(`\n✅ Deleted ${deletedCount} test files`);
    console.log(`✅ Preserved ${realFiles.length} real documents\n`);

    console.log('📂 Remaining real documents:');
    realFiles.forEach(file => {
      const filename = file.Key.split('/').pop();
      const sizeMB = (file.Size / 1024 / 1024).toFixed(2);
      console.log(`   - ${filename} (${sizeMB} MB)`);
    });
  } catch (error) {
    console.error('❌ Error:', error);
    throw error;
  }
}

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