#!/usr/bin/env node

/**
 * Create MinIO bucket for document storage
 *
 * Usage: node scripts/create-minio-bucket.js
 */

const { S3Client, CreateBucketCommand, HeadBucketCommand } = require('@aws-sdk/client-s3');

const bucketName = 'xpeditis-documents';

const s3Client = new S3Client({
  endpoint: 'http://localhost:9000',
  region: 'us-east-1',
  credentials: {
    accessKeyId: 'minioadmin',
    secretAccessKey: 'minioadmin',
  },
  forcePathStyle: true,
});

async function createBucket() {
  try {
    // Check if bucket already exists
    try {
      await s3Client.send(new HeadBucketCommand({ Bucket: bucketName }));
      console.log(`✅ Bucket '${bucketName}' already exists`);
      return;
    } catch (error) {
      if (error.name !== 'NotFound') {
        throw error;
      }
    }

    // Create bucket
    await s3Client.send(new CreateBucketCommand({ Bucket: bucketName }));
    console.log(`✅ Bucket '${bucketName}' created successfully`);
  } catch (error) {
    console.error(`❌ Failed to create bucket:`, error.message);
    process.exit(1);
  }
}

createBucket();
