/**
 * Script to upload test documents to MinIO
 */

const { S3Client, PutObjectCommand, CreateBucketCommand } = require('@aws-sdk/client-s3');
const { Client: PgClient } = require('pg');
const fs = require('fs');
const path = require('path');
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,
});

// Create a simple PDF buffer (minimal valid PDF)
function createTestPDF(title) {
  return Buffer.from(
    `%PDF-1.4
1 0 obj
<<
/Type /Catalog
/Pages 2 0 R
>>
endobj
2 0 obj
<<
/Type /Pages
/Kids [3 0 R]
/Count 1
>>
endobj
3 0 obj
<<
/Type /Page
/Parent 2 0 R
/MediaBox [0 0 612 792]
/Contents 4 0 R
/Resources <<
/Font <<
/F1 <<
/Type /Font
/Subtype /Type1
/BaseFont /Helvetica
>>
>>
>>
>>
endobj
4 0 obj
<<
/Length 100
>>
stream
BT
/F1 24 Tf
100 700 Td
(${title}) Tj
ET
endstream
endobj
xref
0 5
0000000000 65535 f
0000000009 00000 n
0000000058 00000 n
0000000115 00000 n
0000000300 00000 n
trailer
<<
/Size 5
/Root 1 0 R
>>
startxref
450
%%EOF`,
    'utf-8'
  );
}

async function uploadTestDocuments() {
  const pgClient = new PgClient({
    host: process.env.DATABASE_HOST || 'localhost',
    port: process.env.DATABASE_PORT || 5432,
    user: process.env.DATABASE_USER || 'xpeditis',
    password: process.env.DATABASE_PASSWORD || 'xpeditis_dev_password',
    database: process.env.DATABASE_NAME || 'xpeditis_dev',
  });

  try {
    // Connect to database
    await pgClient.connect();
    console.log('✅ Connected to database');

    // Create bucket if it doesn't exist
    try {
      await s3Client.send(new CreateBucketCommand({ Bucket: BUCKET_NAME }));
      console.log(`✅ Created bucket: ${BUCKET_NAME}`);
    } catch (error) {
      if (error.name === 'BucketAlreadyOwnedByYou' || error.Code === 'BucketAlreadyOwnedByYou') {
        console.log(`✅ Bucket already exists: ${BUCKET_NAME}`);
      } else {
        console.log(`⚠️  Could not create bucket (might already exist): ${error.message}`);
      }
    }

    // Get all CSV bookings with documents
    const result = await pgClient.query(
      `SELECT id, documents FROM csv_bookings WHERE documents IS NOT NULL`
    );

    console.log(`\n📄 Found ${result.rows.length} bookings with documents\n`);

    let uploadedCount = 0;

    for (const row of result.rows) {
      const bookingId = row.id;
      const documents = row.documents;

      console.log(`\n📦 Processing booking: ${bookingId}`);

      for (const doc of documents) {
        if (!doc.filePath || !doc.filePath.includes(MINIO_ENDPOINT)) {
          console.log(`   ⏭️  Skipping document (not a MinIO URL): ${doc.fileName}`);
          continue;
        }

        // Extract the S3 key from the URL
        const url = new URL(doc.filePath);
        const key = url.pathname.substring(1).replace(`${BUCKET_NAME}/`, '');

        // Create test PDF content
        const pdfContent = createTestPDF(doc.fileName || 'Test Document');

        try {
          // Upload to MinIO
          await s3Client.send(
            new PutObjectCommand({
              Bucket: BUCKET_NAME,
              Key: key,
              Body: pdfContent,
              ContentType: doc.mimeType || 'application/pdf',
            })
          );

          console.log(`   ✅ Uploaded: ${doc.fileName}`);
          console.log(`      Path: ${key}`);
          uploadedCount++;
        } catch (error) {
          console.error(`   ❌ Failed to upload ${doc.fileName}:`, error.message);
        }
      }
    }

    console.log(`\n🎉 Successfully uploaded ${uploadedCount} test documents to MinIO`);
    console.log(`\n📍 MinIO Console: http://localhost:9001`);
    console.log(`   Username: minioadmin`);
    console.log(`   Password: minioadmin`);
  } catch (error) {
    console.error('❌ Error:', error);
    throw error;
  } finally {
    await pgClient.end();
    console.log('\n👋 Disconnected from database');
  }
}

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