const { Client } = require('minio');

const config = {
  endPoint: 'minio-dev.smart.aaca.gov.et',
  port: 443,
  useSSL: true,
  accessKey: 'f2f22b0ea929cebd5567ed0c71ec351b',
  secretKey: 'xxHnjRsb90suQZZdOtEcXJXls4nj0A2anMetb1kY',
  bucket: 'fhc',
};

const filePath = '/home/marshal/Desktop/EDR/bash/download.jpeg';
const objectName = `test-upload-${Date.now()}.jpeg`;

console.log('Testing MinIO upload...');
console.log('Endpoint:', `${config.useSSL ? 'https' : 'http'}://${config.endPoint}:${config.port}`);
console.log('Bucket:', config.bucket);
console.log('File:', filePath);
console.log('Object:', objectName);
console.log('');

const client = new Client(config);

const fs = require('fs');

try {
  const fileBuffer = fs.readFileSync(filePath);
  console.log('File size:', fileBuffer.length, 'bytes');
  
  client.putObject(config.bucket, objectName, fileBuffer, fileBuffer.length, { 'Content-Type': 'image/jpeg' })
    .then(() => {
      const url = `${config.useSSL ? 'https' : 'http'}://${config.endPoint}:${config.port}/${config.bucket}/${objectName}`;
      console.log('✓ Upload successful!');
      console.log('URL:', url);
    })
    .catch(err => {
      console.error('✗ Upload failed:', err.message);
      if (err.code === 'InvalidAccessKeyId') {
        console.error('The access key does not exist on the MinIO server.');
        console.error('Contact your MinIO administrator for valid credentials.');
      }
    });
} catch (err) {
  console.error('Error reading file:', err.message);
}
